是否可以为AssemblyInfo.cs
使用程序集属性添加信息,例如assembly:MyProjectAssembly,
因此,当 MEF 扫描 rutime 文件夹中的程序集时,它只会扫描那些用MyProjectAssembly
.
我认为MEF没有这个功能。不过,我不是 100% 确定。
您可以做的是使用Mono.Cecil,它是 System.Reflection 的一个非常强大的替代品。Mono.Cecil
允许您在不加载它们的情况下检查(和重写,但这是另一回事).NET 程序集。这意味着您可以轻松添加您正在寻找的功能。例如:
public static bool AssemblyIncludesCustomAttribute(string assemblyPath, string customAttributeName)
{
if (assemblyPath == null) throw new ArgumentNullException("assemblyPath");
if (customAttributeName == null) throw new ArgumentNullException("customAttributeName");
AssemblyDefinition assemblyDef = AssemblyDefinition.ReadAssembly(assemblyPath);
return assemblyDef.CustomAttributes.Any(ca => ca.AttributeType.FullName == customAttributeName);
}
然后像这样使用它:
var catalog = new AggregateCatalog();
string dirPath = @".\Extensions";
foreach (string assemblyFile in Directory.EnumerateFiles(dirPath, "*.dll"))
{
if (AssemblyIncludesCustomAttribute("Blah.dll", "System.Reflection.AssemblyConfigurationAttribute"))
{
catalog.Catalogs.Add(new AssemblyCatalog(assemblyFile));
}
}