CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Reflection to execute a method implemented from interface

  1. #1
    Join Date
    Nov 2014
    Posts
    1

    Post Reflection to execute a method implemented from interface

    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.

    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.

    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.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Reflection to execute a method implemented from interface

    Ok,it's friday and I've had a few beers but frankly I just get confused here. Anyway, shouldn't this post be in c#or some similar forum?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)