2013-01-02 125 views

回答

0

我不認爲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));      
       } 
      }