Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I write a program in c# and a dll plugin.
I want to write code in the form_load method that search dll files in program directory.
Then append this dll files and run these.
I know that this actions can be done with "system.reflection" and "system.io", but i dont know how do it!
Posted
Comments
Tomas Takac 5-May-15 13:45pm    
Why don't you try to do it yourself? Then come here and ask specific questions if you hit a problem. BTW, MEF[^] can do exactly that.

1 solution

"Append a DLL" is nothing but absurd. You can always load an assembly (assembly, not "DLL") using Reflection:
https://msdn.microsoft.com/en-us/library/system.reflection.assembly%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/1009fa28(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/ky3942xh(v=vs.110).aspx[^]… see all other Assembly.Load* methods.

Then you can use Assembly.LoadTypes or other similar method to get types: https://msdn.microsoft.com/en-us/library/system.reflection.assembly.gettypes(v=vs.110).aspx[^].

With the type, you can get the members you need and, in particular, constructors; then you can instantiate any type using System.Reflection.ConstructorInfo:
https://msdn.microsoft.com/en-us/library/system.reflection.constructorinfo%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.reflection.constructorinfo.invoke%28v=vs.110%29.aspx[^].

And then do whatever you want.

It would be the best to base getting the the type you need by using some declarations used by both host and loaded assemblies. One of the best option is to find the type by the interface it is supposed to implement. For further detail, please see my past answers:
Gathering types from assemblies by it's string representation[^],
Access a custom object that resides in plug in dll[^].

Note that the assembly loaded during runtime is not the part of your project, otherwise doing it dynamically would not make any sense at all; you could simply reference the assembly and use it in a usual way. Dynamically, you load assembly either using the file name of the assemblies main module (the one with .NET assembly manifest) or from GAC by its strong name.

—SA
 
Share this answer
 
v2
Comments
Tomas Takac 5-May-15 13:58pm    
+5 for the effort
Sergey Alexandrovich Kryukov 5-May-15 14:56pm    
Thank you, Tomas.
—SA
Matt T Heffron 5-May-15 16:30pm    
+5
Sergey Alexandrovich Kryukov 5-May-15 17:05pm    
Thank you, Matt.
—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