4
我想編寫一個程序來查找.NET程序集中的所有com可見.NET類和它們的ProgID。什麼是最好的方法來做到這一點?從.NET程序集中獲取ProgID
我想編寫一個程序來查找.NET程序集中的所有com可見.NET類和它們的ProgID。什麼是最好的方法來做到這一點?從.NET程序集中獲取ProgID
這將讓該PROGID而不是classid和如果整個裝配標記也可見工作:
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));
}
}
}
喜歡它;我會刪除我答覆尊敬... – 2009-04-10 21:30:56