2009-04-10 8 views
4

Ich möchte ein Programm schreiben, um alle com sichtbaren .NET-Klassen und ihre ProgIDs von einer .NET-Assembly zu finden. Was ist der beste Weg, dies zu tun?Get ProgID von .NET Assembly

Antwort

7

Dies wird die ProgId statt dem ClassId bekommen, und arbeitet auch, wenn die gesamte Baugruppe sichtbar gekennzeichnet ist:

 Assembly assembly = Assembly.LoadFile("someassembly.dll"); 

     bool defaultVisibility; 
     object[] assemblyAttributes = assembly.GetCustomAttributes(typeof(ComVisibleAttribute),false); 
     if (assemblyAttributes.Length == 0) 
      defaultVisibility = false; 
     else 
      defaultVisibility = (assemblyAttributes[0] as ComVisibleAttribute).Value; 

     foreach(Type type in assembly.GetTypes()) 
     { 
      bool isComVisible = defaultVisibility; 
      object []attributes = type.GetCustomAttributes(typeof(ComVisibleAttribute),true); 
      if (attributes.Length > 0) 
       isComVisible = (attributes[0] as ComVisibleAttribute).Value; 
      if (isComVisible) 
      { 
       attributes = type.GetCustomAttributes(typeof(ProgIdAttribute),true); 
       if (attributes.Length >0) 
       { 
        Console.WriteLine(String.Format("Type {0} has ProgID {1}",type.Name,(attributes[0] as ProgIdAttribute).Value)); 
       } 
      } 
     } 
+0

Wie es; Ich lösche mich antworte in Ehrerbietung ... –

+0

Danke, dass ich das Bit über fehlte, wenn die gesamte Baugruppe als com sichtbar markiert ist –