从 .NET 程序集获取 ProgID

发布于 2024-07-17 07:44:32 字数 66 浏览 6 评论 0原文

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

I want to write a program to find all the com visible .NET classes, and their ProgIDs from a .NET assembly. What's the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡绿茶香 2024-07-24 07:44:32

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

This will get the ProgId rather than the ClassId, and also work if the whole assembly is marked visible:

        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));
                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文