繁体   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