gpt4 book ai didi

c# - 使用 MEF 将程序集添加到目录

转载 作者:太空狗 更新时间:2023-10-30 01:24:21 25 4
gpt4 key购买 nike

有一个相当大的项目,将近十几个程序集添加到 shell 中,其他程序集通过配置添加

试图找到一种更好的方法来添加项目中引用的程序集

目前的代码是这样的

 Dictionary<string,string> dict = new Dictionary<string,string>(); // to avoid duplicate parts
foreach (Assembly an in AppDomain.CurrentDomain.GetAssemblies())
addAssembly(an, dict);

foreach (AssemblyName an in Assembly.GetEntryAssembly().GetReferencedAssemblies())
addAssembly(an, dict);

foreach (AssemblyName an in GetAsmInConfig())
addAssembly(an, dict);

即便如此,并非所有在设计时引用到 Shell 的程序集都添加到目录中

想知道除了将所有程序集添加到配置中是否还有其他方法可以解决问题

更新

  void addAssembly(Assembly a, Dictionary<string, string> dict)
{
if (a.IsDynamic || dict.ContainsKey(a.FullName))
return;

dict.Add(a.FullName, a.FullName);
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(a));
}

最佳答案

编译器将删除对未实际使用的引用,因此您不能依赖它作为枚举程序集的机制。您的选择是:

  1. 使用配置文件
  2. 使用目录目录

关于c# - 使用 MEF 将程序集添加到目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9654930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com