2009-04-10 10 views
4

comのすべての表示可能な.NETクラス、およびそれらのProgIDを.NETアセンブリから検索するプログラムを作成します。これを行う最善の方法は何ですか?.NETアセンブリからProgIDを取得

答えて

7

これはCLASSIDではなく、プログラムIDを取得し、アセンブリ全体が見えるマークされている場合にも動作します。それのように

 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

。私は敬意の中で私の答えを削除します... –

+0

ありがとう –

関連する問題