【发布时间】:2012-09-26 20:35:05
【问题描述】:
我正在开发一些支持使用报告模块的软件。基本上,我们不想默认发送所有报告,因为它只会造成不必要的膨胀,所以我正在寻找一种有效的方法来检查当前的 exe 和 dll 是否有可以在运行时动态添加的有效模块。到目前为止,我已经尝试在 AssemblyInfo 中设置自定义属性并将 at 用作如下标志:
private void GetReportModules() {
try {
var dlls = Directory.GetFiles(Directory.GetCurrentDirectory()).Where(x => Path.GetExtension(x) == ".dll" || Path.GetExtension(x) == ".exe");
dlls = dlls.OrderBy(x => Path.GetFullPath(x)).Where(x => {
try {
return null != System.Reflection.Assembly.LoadFrom(x);
} catch (Exception) {
Console.WriteLine("Skipping {0} - does not load using reflection.", Path.GetFileName(x));
return false;
}
});
foreach (var dll in dlls) {
System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(dll);
object[] attributes = assembly.GetCustomAttributes(typeof(ReportNameAttribute), false);
if (attributes.Count() > 0) {
// dynamically include the assembly as a menu item
}
}
} catch (Exception ex) { throw new GenericException(ex); }
}
问题是我实际上必须加载每个 dll 和 exe 两次,这似乎效率不高。任何人都可以提出一种他们以前完成过类似事情的方法或改进我正在做的事情的方法吗?
如果我无需先加载程序集就可以检查属性,那将是非常棒的。
【问题讨论】:
-
您能否在每个解决方案中根据需要包含您需要的库?对于我的客户,我为每个客户提供了一个解决方案,这样只包含他们需要的库。基本代码引用自主解决方案,但所有客户端代码都与客户端解决方案隔离。
-
不,这行不通,我的任务是动态执行,所以必须交付。我所说的很多报告可能会在内部用于不同的开发人员和各种目的,所以老板希望这样做
标签: c# wpf dll module .net-assembly