I have the following code:
var type = typeof(PluginInterface.iMBDDXPluginInterface);
var types = AppDomain.CurrentDomain.GetAssemblies().ToList()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p));
Type t = types.ElementAt(0);
PluginInterface.iMBDDXPluginInterface instance = Activator.CreateInstance(t) as PluginInterface.iMBDDXPluginInterface;
TabPage tp = new TabPage();
tp = instance.pluginTabPage();
这个dll内的类实现了PluginInterface接口,上面的代码中Type是正确的类/类型,但是当我尝试通过接口创建一个实例时,出现了以下错误信息:
对象引用没有设置到对象的实例。
有人知道原因吗?
谢谢。
Activator.CreateInstance(t) as Plugin...
改为(Plugin...) Activator.CreateInstance(t)
。 - Tim Robinson