从.NET程序集中获取ProgID(Get ProgID from .NET Assembly)

编程入门 行业动态 更新时间:2024-10-25 00:30:06
从.NET程序集中获取ProgID(Get ProgID from .NET Assembly)

我想编写一个程序来查找所有可见的.NET类,以及它们来自.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?

最满意答案

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

更多推荐

本文发布于:2023-07-26 19:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279893.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   NET   Assembly   ProgID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!