【发布时间】:2013-11-27 19:53:30
【问题描述】:
目前我正在使用反射在我的程序集中搜索实现接口的类,然后检查这些类的名称以查看它是否与正在搜索的类匹配。
我的下一个任务是在此代码中添加一种在目录中搜索 DLL 文件的方法,我唯一的提示是我可以使用“System.Environment.CurrentDirectory”。我还需要考虑并非所有 DLL 都包含 .net 程序集这一事实。
谁能推荐从哪里开始?
IInstruction instruction = null;
string currentDir = Environment.CurrentDirectory;
var query = from type in Assembly.GetExecutingAssembly().GetTypes()
where type.IsClass && type.GetInterfaces().Contains(typeof(IInstruction))
select type;
foreach (var item in query)
{
if (opcode.Equals(item.Name, StringComparison.InvariantCultureIgnoreCase))
{
instruction = Activator.CreateInstance(item) as IInstruction;
return instruction;
}
}
opcode 是我正在搜索的类的名称。
【问题讨论】:
-
您可以使用this 作为起点,当然指向您自己的目录而不是框架程序集目录。
-
@JoachimIsaksson 您能否更详细地解释您的答案,甚至可以举个例子?
标签: c# reflection dll