Just based on a response to minitech. If you can use C # 4.0, you can omit some reflection calls.
public static void Main() { Assembly ass = Assembly.LoadFile(@"PathToLibrar\ClassLibraryTest.dll"); var type = ass.GetType("ClassLibrary1.Calculator"); dynamic instance = Activator.CreateInstance(type); int add = instance.Calc(1, 3); }
Here, as an instance type dynamic , you don't need to look for the Calc method by reflection.
But the best way is to define the interface upstream
public interface ICalculator { int Calc(int i, int b); }
and implement it in its class downstream
public class Calculator : ICalculator { public int Calc(int i, int b) { return i + b; } }
Then you can do the reflection minimally to build the object.
public static void Main() { Assembly ass = Assembly.LoadFile(@"PathToLibrar\ClassLibraryTest.dll"); var type = ass.GetType("ClassLibrary1.Calculator"); ICalculator instance = Activator.CreateInstance(type) as ICalculator; int add = instance.Calc(1, 3); }
This will give you better performance.
source share