Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have 2 dll's .

1)AbInterface.dll which has interface "InterfaceStart" and a method start() inside the interface . There are many classes too inside this dll.
2)AbClass.dll which has a class "AbClassStart" that implements "InterfaceStart" and does have a definition for Start() method in the class.

Now my project is just supposed to load the AbInterface.dll along with some other dll's to perform the intended functionality.

The AbInterface.dll has an abstract class Ab.AbInterface.MyClass.init() which executes the start() method of AbClassStart.dll by using below code.

C#
string assemblyClassPath = "D:\\XYZ\\ABClass.dll";
InterfaceStart startService = FindStartable(assemblyPath);
startService.start();

 private static InterfaceStart FindStartable(string assemblyPath)
      {
              Assembly theAssembly = Assembly.LoadFrom(assemblyPath);
              Type[] mytypes = theAssembly.GetTypes();
              foreach (Type t in mytypes)
              {
                  Type theStartableType;
                  theStartableType =t.GetInterface("AB.AbInterface.InterfaceStart");
                  if (theStartableType != null)
                  {
                      InterfaceStart res =      (InterfaceStart)theAssembly.CreateInstance(t.FullName);
                      return res;
                  }
              }
              return null;
          }
      }


Since the assemblypath referred by AbInterface.dll is different and i cannot change it , i decided to write above code in my project and just change the assembly path to the desired one.

i am able to successfully execute it and getting the intended result when i add reference of both dll's in my project and copy paste above lines in my project.

But i have a constraint in my implementation ie i can't have direct references of these dll's in my project and hence i m trying to use reflection in executing the same code.

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.

C#
string assemblyPath = "D:\\APP\\AbInterface.dll";
string assemblyClassPath = "D:\\APP\\AbClass.dll";
Assembly assembly = Assembly.LoadFrom(assemblyPath);
Assembly assemblyClass = Assembly.LoadFrom(assemblyClassPath);
Type AbserveStart = assembly.GetType("AB.AbInterface.InterfaceStart");
Type classstart = assemblyClass.GetType("AB.AbClass.AbClassStart");
MethodInfo startMethod = AbserveStart.GetMethod("Start");
object interfaceObject = Activator.CreateInstance(classstart);
startMethod.Invoke(interfaceObject, null);


Also another thing is even when i tried adding a direct reference of ABClass.DLL in my project, i observed that AbClass.AbClassStart.Start() method couldn't be accessed and start() method was executed only by using interface object as shown in the top.
Posted
Updated 27-Nov-14 7:38am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900