【问题标题】:Get ProgID from .NET Assembly从 .NET 程序集获取 ProgID
【发布时间】:2009-04-10 20:04:39
【问题描述】:

我想编写一个程序来从 .NET 程序集中查找所有 com 可见的 .NET 类及其 ProgID。最好的方法是什么?

【问题讨论】:

    标签: .net reflection com .net-2.0


    【解决方案1】:

    这将获得 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));
                    }
                }
            }
    

    【讨论】:

    • 谢谢我错过了关于整个程序集是否标记为 com 可见
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    • 2018-08-30
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 2011-03-03
    相关资源
    最近更新 更多