簡體   English   中英

從DLL實例化的類似乎未正確實現接口

[英]Class instantiated from DLL does not seem to be implementing interface properly

我有一個試圖通過使用Assembly和Activator實例化的類,該類實現了一個接口,但是當我通過檢查該類是否實現了該條件的條件來運行該類的實例時,我得到了錯誤。 可能是什么問題呢?

我正在檢查實現的代碼:

     string className = MyClass;
     Type type = null;
     Assembly assembly = Assembly.LoadFile("@C:\\MyDLL", new Evidence(AppDomain.CurrentDomain.Evidence));
     type = assembly.GetType(className);
     object instance = Activator.CreateInstance(type);

     //never makes it past this conditional
     if (!(instance is MyInterface)
     {
     //It always endsup in here, when it shouldn't.
     System.Writeline("ERROR");
     }
     else{
     //This is what needs to happen
     }

MyClass類的代碼超出了所有這些范圍,並且不在MyDLL中

public class MyClass: MyInterface
{
//Contents irrelevent to my problem
}

我不知道為什么這沒有通過條件。 我可以實例化該類錯誤嗎? 還需要注意的是,在使用Assembly / Activator和使用接口方面,我是一個非常新手。

最可能的原因-DLL和您的代碼都有自己的MyInterface版本。 這可能是因為

  • 一個人不想花時間為界面想出一個很好的唯一名稱,
  • 有人決定共享接口作為源,而不是通過程序集引用,
  • 在不同名稱空間中using良好的命名接口,而您using是錯誤的接口。

您可能直接引用了程序集。 如果是這樣,則動態加載的類型將具有相同的名稱和名稱空間,但運行時會認為它們是不同的。

暫無
暫無

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

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