简体   繁体   中英

Load a Specific class from dll in C#

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM