我有以下界面:
public interface PluginInterface<T> where T : MyData
{
List<T> GetTableData();
}
Run Code Online (Sandbox Code Playgroud)
在一个单独的程序集中,我有一个实现此接口的类.实际上,实现此接口的所有类都在单独的程序集中.原因是将我的应用程序设计为插件主机,其中插件可以在将来完成,只要它们实现上述接口并将程序集DLL复制到相应的文件夹即可.
我的应用程序通过首先加载程序集来发现插件并执行以下操作:
List<PluginInterface<MyData>> Plugins = new List<PluginInterface<MyData>>();
string FileName = ...;//name of the DLL file that contains classes that implement the interface
Assembly Asm = Assembly.LoadFile(Filename);
foreach (Type AsmType in Asm.GetTypes())
{
//Type type = AsmType.GetInterface("PluginInterface", true); //
Type type = AsmType.GetInterface("PluginInterface<T>", true);
if (type != null)
{
PluginInterface<MyData> Plugin = (PluginInterface<MyData>)Activator.CreateInstance(AsmType);
Plugins.Add(Plugin);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是因为我通过Type type = ...来获取类型的行似乎都不起作用,因为两者似乎都是null.我觉得通用以某种方式造成了麻烦.你知道为什么吗?
在查询接口时,您似乎必须使用损坏的接口名称:
Type type = AsmType.GetInterface("PluginInterface`1", true);
Run Code Online (Sandbox Code Playgroud)
来自MSDN:
对于通用接口,name参数是受损的名称,以重音符号(`)和类型参数的数量结尾.对于通用接口定义和构造的通用接口都是如此.例如,要查找IExample <T>或IExample <string>,请搜索"IExample`1".
| 归档时间: |
|
| 查看次数: |
1376 次 |
| 最近记录: |