Col*_*e W 10
我实现了类似于你要求通过给定目录中的dll搜索并找到实现特定接口的类的东西.以下是我以前用过的课程:
public class PlugInFactory<T>
{
public T CreatePlugin(string path)
{
foreach (string file in Directory.GetFiles(path, "*.dll"))
{
foreach (Type assemblyType in Assembly.LoadFrom(file).GetTypes())
{
Type interfaceType = assemblyType.GetInterface(typeof(T).FullName);
if (interfaceType != null)
{
return (T)Activator.CreateInstance(assemblyType);
}
}
}
return default(T);
}
}
Run Code Online (Sandbox Code Playgroud)
您所要做的就是使用以下内容初始化此类:
PlugInFactory<InterfaceToSearchFor> loader = new PlugInFactory<InterfaceToSearchFor>();
InterfaceToSearchFor instanceOfInterface = loader.CreatePlugin(AppDomain.CurrentDomain.BaseDirectory);
Run Code Online (Sandbox Code Playgroud)
如果此答案或任何其他答案可以帮助您解决问题,请单击复选标记将其标记为答案.此外,如果您觉得这是一个很好的解决方案,请表达您的赞赏.我以为我会提到它,因为它看起来不像你接受任何其他问题的答案.
| 归档时间: |
|
| 查看次数: |
9463 次 |
| 最近记录: |