Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 托管可扩展框架和AssemblyInfo.cs_C#_Mef - Fatal编程技术网

C# 托管可扩展框架和AssemblyInfo.cs

C# 托管可扩展框架和AssemblyInfo.cs,c#,mef,C#,Mef,是否可以将信息添加到AssemblyInfo.cs使用assembly属性,例如assembly:MyProjectAssembly, 因此,当MEF扫描rutime文件夹中的程序集时,它将只扫描那些用MyProjectAssembly装饰的程序集,我认为MEF没有此功能。但我不是100%肯定 您可以做的是使用,这是一个非常强大的System.Reflection替代品Mono.Cecil允许您在不加载.NET程序集的情况下检查(和重写,但那是另一回事)。这意味着您可以轻松添加所需的功能。例如:

是否可以将信息添加到
AssemblyInfo.cs
使用assembly属性,例如
assembly:MyProjectAssembly,


因此,当MEF扫描rutime文件夹中的程序集时,它将只扫描那些用
MyProjectAssembly

装饰的程序集,我认为MEF没有此功能。但我不是100%肯定

您可以做的是使用,这是一个非常强大的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));                    
                }
            }