0

I have an application that needs to load and execute assemblies at runtime.

The Add-In or Plug-In approach will not work in my case since the number of assemblies will be in the hundreds and the appropriate ones would be loaded and executed at runtime.

I am looking for two things:

  • Sample code to load and execute assemblies whose internal structure is known to me at compile time.
  • Best practices to ensure stability performance and security or authenticity of loaded assemblies.

My application code logic will look like this:

public static void Main()  
{  
    foreach (string filename in directory.GetFiles())  
    {  
        Assembly assembly = LoadAssemblyFromFile(filename);  
        object instance = CreateInstanceOfClassXyzInAssembly(assembly);  
        instance.CallSomeMethodWithKnownInterface();  
        instance = null;  
        UnloadAssembly(assembly);  
    }  
}
jcopenha
  • 3,935
  • 1
  • 17
  • 15
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
  • Now Reflection is costly, i hope you know that. Second, your assemblies where referred in the code, the CLR shall load them and does JITting and then it unloads if its over. So why you would like to do that yourself? – Zenwalker Apr 12 '12 at 10:16
  • see this post - http://stackoverflow.com/questions/1137781/c-sharp-correct-way-to-load-assembly-find-class-and-call-run-method – Rajeesh Apr 12 '12 at 10:21
  • In addition to zenwalkers comment: also note that the saambly will not be unloaded unless you unload the whole appdomain. See: [How to: Load and unload Assemblies](http://msdn.microsoft.com/en-us/library/ms173101%28v=vs.100%29.aspx) – W van Noort Apr 12 '12 at 13:41

0 Answers0