簡體   English   中英

在C#中從DLL加載特定類

[英]Load a Specific class from dll in C#

我只想從給定的dll加載某些類。 問題如下:

我有以下dll文件。

DLL 1:

命名空間:

  • 1類
  • 2級

DLL 2:

命名空間:

  • 2級
  • 3級

如上面的示例所示,我有2個甚至更多個具有相同類的dll是有可能發生的。 (請注意,名稱空間相同)

現在我想到了以下幾點:

  • 在Temp AppDomain中打開一個dll
  • 檢查我們不知道的課程
  • 將所需的類移至標准Appdomain
  • 卸載臨時Appdomain

有什么辦法可以做這樣的事情嗎?

您有兩個不同的dll文件。 因此,您可以將兩個不同的dll文件導入到C#項目中。 例如,第一個dll文件的名稱為“ File1”,第二個dll文件的名稱為“ 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();

注意:不要忘記添加“不安全”的C#表單。 像這樣

unsafe public partial class FORMNAME : Form

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM