Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to access a class and its methods, properties, etc. from dynamically compiled code using the code dom. I can create and compile code fine using the code dom but I cant seem to get access to a class that is outside of the code dom compiled class. Is there a way I can add an object reference to the code dom to give it access to the class? Sort of like the MSScript Control allows you to add an object?
Posted

1 solution

You can do it via reflection:
http://en.wikipedia.org/wiki/Reflection_(computer_programming)[^],
https://msdn.microsoft.com/en-us/library/System.Reflection%28v=vs.110%29.aspx[^].

Ideally, you need to define some commonly-known interface (let me call it "plug-in interface") and find the class or structure implementing this interface in your assembly obtained from CodeDOM. Then you can instantiate this type and work with its instance. Avoid identifying anything by string names; it would lead to unsupportable code. This is just the basic technique; there is a lot more to it. Please see my past answers:
code generating using CodeDom[^],
Access a custom object that resides in plug in dll[^],
Dynamically Load User Controls[^],
C# Reflection InvokeMember on existing instance[^],
Gathering types from assemblies by it's string representation[^],
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^].

—SA
 
Share this answer
 
Comments
Member 11551709 21-Oct-15 7:58am    
Thank you for your answer. I do have an interface and I have created an instance to call the code compiled with CodeDom, but what I can't seem to figure out is to call a method that resides in my client application from the compiled code inside the CodeDom. I hope that I haven't misunderstood your answer. If it would be helpful I can post some code snippets.
Sergey Alexandrovich Kryukov 21-Oct-15 9:40am    
I explained exactly what to do. Here is the thing: when you make sure that some type implements your interface and is instantiated, you can typecast the instance to your interface type. Than you can call your method of this interface type in this instance, as everything else prescribed in this interface.
—SA
Member 11551709 21-Oct-15 10:21am    
Ahhh I understand now. Thank you for your time and help!
Sergey Alexandrovich Kryukov 21-Oct-15 10:58am    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900