Skip to main content
removed "assumes no parameters" in example using parameters
Source Link
Lasse V. Karlsen
  • 393.9k
  • 107
  • 651
  • 845
Object[] arguments = new Object[] { arg1, arg2, arg3 ... }; method.Invoke(null, arguments); // assumes no parameters 
Object[] arguments = new Object[] { arg1, arg2, arg3 ... }; method.Invoke(null, arguments); // assumes no parameters 
Object[] arguments = new Object[] { arg1, arg2, arg3 ... }; method.Invoke(null, arguments); 
added note about assembly on disk
Source Link
Lasse V. Karlsen
  • 393.9k
  • 107
  • 651
  • 845

This assumes you have the assemblies on disk as files. If you don't, like if you get them from a database as a byte array, there are other methods on the Assembly that will help you give you an Assembly object after loading it.

To iterate through all the classes in the assembly, you would do this:

Assembly assembly = Assembly.LoadFile(@"test.dll"); var method = (from type in assembly.GetTypes() where type.IsClass let onStartMethod = type.GetMethod("OnStart", BindingFlags.Static | BindingFlags.Public) where onStartMethod != null select onStartMethod).FirstOrDefault(); if (method != null) { method.Invoke(null, new Object[0]); // assumes no parameters } 

To iterate through all the classes in the assembly, you would do this:

var method = (from type in assembly.GetTypes() where type.IsClass let onStartMethod = type.GetMethod("OnStart", BindingFlags.Static | BindingFlags.Public) where onStartMethod != null select onStartMethod).FirstOrDefault(); if (method != null) { method.Invoke(null, new Object[0]); // assumes no parameters } 

This assumes you have the assemblies on disk as files. If you don't, like if you get them from a database as a byte array, there are other methods on the Assembly that will help you give you an Assembly object after loading it.

To iterate through all the classes in the assembly, you would do this:

Assembly assembly = Assembly.LoadFile(@"test.dll"); var method = (from type in assembly.GetTypes() where type.IsClass let onStartMethod = type.GetMethod("OnStart", BindingFlags.Static | BindingFlags.Public) where onStartMethod != null select onStartMethod).FirstOrDefault(); if (method != null) { method.Invoke(null, new Object[0]); // assumes no parameters } 
Source Link
Lasse V. Karlsen
  • 393.9k
  • 107
  • 651
  • 845

To load the assembly, you would do this:

Assembly assembly = Assembly.LoadFile(@"test.dll"); 

To iterate through all the classes in the assembly, you would do this:

Assembly assembly = Assembly.LoadFile(@"test.dll"); foreach (Type type in assembly.GetTypes()) { if (type.IsClass) { ... } } 

To find the OnStart static method, you would do this:

Assembly assembly = Assembly.LoadFile(@"test.dll"); foreach (Type type in assembly.GetTypes()) { if (type.IsClass) { MethodInfo method = type.GetMethod("OnStart", BindingFlags.Static | BindingFlags.Public); if (method != null) { ... } } } 

To call the method, you would do this:

Assembly assembly = Assembly.LoadFile(@"test.dll"); foreach (Type type in assembly.GetTypes()) { if (type.IsClass) { MethodInfo method = type.GetMethod("OnStart", BindingFlags.Static | BindingFlags.Public); if (method != null) { method.Invoke(null, new Object[0]); // assumes no parameters break; // no need to look for more methods, unless you got multiple? } } } 

If you need to pass arguments to the method, you would put them in an object array:

Object[] arguments = new Object[] { arg1, arg2, arg3 ... }; method.Invoke(null, arguments); // assumes no parameters 

The above code can be collapsed to the following by using Linq to find the method for us:

var method = (from type in assembly.GetTypes() where type.IsClass let onStartMethod = type.GetMethod("OnStart", BindingFlags.Static | BindingFlags.Public) where onStartMethod != null select onStartMethod).FirstOrDefault(); if (method != null) { method.Invoke(null, new Object[0]); // assumes no parameters }