3
Answers

Reflection to call method in class implementing interface

Njn Bat

Njn Bat

9y
1.1k
1
I have 2 dll's . One Dll has interface "InterfaceStart" . Second dll has a class "AbServicesStart" which implements the interface.
 
I must create the instance of the class and call the implemented start method . 
 
I have tried below code how ever i get an exception saying A first chance exception of type 'System.Reflection.TargetException' occurred in mscorlib.dll Additional information: Object does not match target type . 
 
Can u help me figure out the issue.  
 
string assemblyPath = "D:\\APP\\AbInterface.dll";  
string assemblyClassPath = "D:\\APP\\ABServe.dll";
Assembly assembly = Assembly.LoadFrom(assemblyPath);
Assembly assemblyClass = Assembly.LoadFrom(assemblyClassPath);
Type AbserveStart = assembly.GetType("AB.ABServe.InterfaceStart");
Type classstart = assemblyClass.GetType("AB.AbInterface.AbServicesStart");
MethodInfo startMethod = AbserveStart.GetMethod("Start");
object interfaceObject = Activator.CreateInstance(classstart);
startMethod.Invoke(interfaceObject, null);
 
 
Answers (3)