How to call a static method in different assembly using Reflection in C# and VB.NET

To call a static method in different assembly using Reflection in C# and VB.NET you can use the snippet below.

Sample C#

Assembly assembly = Assembly.LoadFile(@"Assembly_Location"); 
var t = assembly.Types().FirstOrDefault(x=>x.FullName.Contains("MyClassName"));
var method = t.GetMethod("MyMethodName", BindingFlags.Public | BindingFlags.Static);
method.Invoke(null, null); //assuming that the method does not have parameters
method.Invoke(null, new object[] { param1}); //assuming that the method has 1 parameter

Sample VB.NET

Dim assembly__1 As Assembly = Assembly.LoadFile("Assembly_Location")
Dim t = assembly.Types().FirstOrDefault(Function(x) x.FullName.Contains("MyClassName"))
Dim method = t.GetMethod("MyMethodName", BindingFlags.[Public] Or BindingFlags.[Static])
method.Invoke(Nothing, Nothing) 'assuming that the method does not have parameters
method.Invoke(Nothing, New Object() {param1}) 'assuming that the method has 1 parameter

Please Share us:
Please rate this snippet

5 thoughts on “How to call a static method in different assembly using Reflection in C# and VB.NET

Leave a Reply