2

I want to load only some classes from a given dll. The problem is the following:

I have the following of dll files.

Dll 1:

Namespace:

  • Class 1
  • Class 2

Dll 2:

Namespace:

  • Class 2
  • Class 3

As shown in the example above, it is possible and likely to happen that I have 2 or even more dlls with the same classes in it. (Note that the namespaces are the same)

Now I thought of the following:

  • Open a dll in a Temp AppDomain
  • Check which classes we do not know
  • Move needed classes to Standard Appdomain
  • Unload Temp Appdomain

Is there any way to do something like that?

Clay
  • 4,999
  • 1
  • 28
  • 45
Dropye
  • 214
  • 3
  • 18
  • Are you asking about late binding? I had a system I worked on that late binded a DLL (a master VB.net project that late binded a DLL from a project written in C#) which we then called a function by string name within that DLL to start the ball rolling (it was an automated telephone system). – DanAbdn Feb 11 '19 at 14:31
  • Are the dlls under your control? – Fildor Feb 11 '19 at 14:32
  • @Fildor Every dll is autogenerated from a Programm I have full control of – Dropye Feb 11 '19 at 14:34
  • Try to use _extern alias_ This thread can help https://stackoverflow.com/questions/3672920/two-different-dll-with-same-namespace – Pavel Anikhouski Feb 11 '19 at 14:42
  • When do you know which version of any conflicting classes you will need (Namespace.Class2 in your example above)....not until run time? – Richard II Feb 11 '19 at 14:50
  • @RichardII this is the exact Problem, I do not know, until run time – Dropye Feb 11 '19 at 14:56
  • Will the conflicting classes (Class2 in your example) have the same public interface, i.e., same public (and "internal") method names and signatures, same public properties, etc? If not, without a code sample, I have trouble grasping how you plan to use this. – Richard II Feb 11 '19 at 19:30
  • Different DLLs using the same class names sounds close to using a plug-in mechanism. [Here is a tutorial how to](https://code.msdn.microsoft.com/windowsdesktop/Creating-a-simple-plugin-b6174b62). – axuno Feb 11 '19 at 20:33

1 Answers1

0

You have two different dll file. So You can import two different dll file into you C# project. For example first dll file's name is "File1" and second dll file's name is "File2"

const string file1_Dll_Path = @"File1.dll";
const string file2_DllPath = @"File2.dll";
[DllImport(file2_DllPath, CallingConvention = CallingConvention.Cdecl)]
    public static extern int YOURFUNCTIONINFILE2DLL();
[DllImport(file1_Dll_Path, CallingConvention = CallingConvention.Cdecl)]
    public static extern void YOURFUNCTIONINFILE1DLL();

Note: Don't forget adding "unsafe" your C# Form. Like this

unsafe public partial class FORMNAME : Form
Cevdet
  • 69
  • 9
  • This would help if i knew wich classes I have to load on Build time. But I know only at run time wich classes I need – Dropye Feb 11 '19 at 15:03