1

This question seems to be a common one but I'm still struggling to find a correct solution. I'm not sure whether its doable or not.Could some one help me please!

I have a .net winform application (TestApp) which holds a reference to a dll called MyDll. This MyDll inturn inherits classes from a 3rd party dll(xyz).

SendMsg send = New SendMsg();

Here SendMsg is a MyDll class which inherits ne_SendMessage class of the 3rd party dll. I'm trying to create an instance for SendMsg class in TestApp.

Now my scenario is, I should not add direct reference to the 3rd party dll(XYZ) into my application TestApp because XYZ.dll gets updated often. If i remove its reference, Im getting an error "the type XYZ.class is defined in an assembly which is not referenced.You must add a reference to assembly XYZ".

Is it possible to overcome this error by adding the XYZ.dll dynamically using reflection? If yes how can i do it?

Incase my question is unclear please leave your comments. Thanks in advance!

  • Add a code snippet so we know exactly what you've tried. This will help us understand better where to guide you – Dan D Aug 10 '17 at 22:54
  • SendMsg send = New SendMessage() – user7380740 Aug 10 '17 at 22:57
  • Here SendMsg is a MyDll class which inherits ne_SendMessage class of the 3rd party dll. I'm trying to create an instance for SendMsg class. – user7380740 Aug 10 '17 at 22:59
  • Why are the types different: SendMsg and SendMessage()? Shouldn't it be SendMsg send = new SendMsg();? – CodexNZ Aug 10 '17 at 22:59
  • I'm sorry , that was a typo . – user7380740 Aug 10 '17 at 23:02
  • Have a look at this post: https://stackoverflow.com/questions/20660999/the-type-is-defined-in-an-assembly-that-is-not-referenced-how-to-find-the-cause – CodexNZ Aug 10 '17 at 23:09
  • Thank you thats a good one to point out, Its due to dependency issue . That particular constructor is inheriting a constructor of another class from a different 3rd party dll apart from the ne_SendMessage. – user7380740 Aug 10 '17 at 23:20
  • But can i resolve it if i try to load that dll dynamically ? – user7380740 Aug 10 '17 at 23:25
  • You could reference the 'other' thrid party dll inside your 'MyDll' which should allow you to achieve what you are trying to do. I have just tried a similar thing in a sample and it stops complaining. This may be easier than loading the dll dynamically unless there is a specific reason for doing so. – CodexNZ Aug 10 '17 at 23:35
  • It does have a reference for for MetaData Dll (the other). – user7380740 Aug 10 '17 at 23:53
  • 2
    Sounds like you may need to do some refactoring to contain the third party dependencies to the MyDll.dll. If you are able to contain the dependencies inside the dll and provide a clean interface to any consuming application, all that app should need to reference is the MyDll.dll. Consider using a factory pattern to generate and return the types you need from inside the MyDll project or a class that you can call to do the sending for you using the required params. – CodexNZ Aug 10 '17 at 23:59

1 Answers1

0

Don´t know if i got it, but try this way:

        var assemblyPath = Path.Combine(Directory.GetCurrentDirectory(), "XYZ.dll");
        var assembly = Assembly.LoadFile(assemblyPath);
        var moduleType = assembly.GetType("XYZ");

        var xyz = Activator.CreateInstance(moduleType);

If you want to reflective inhire a class you may can compile your SendMsg.dll in runtime, see Compiling C# code at runtime or C# Compile at Runtime Visual Studio unusual behavior

Felix Arnold
  • 839
  • 7
  • 35