gpt4 book ai didi

.net - 从 .NET 程序集获取 ProgID

转载 作者:行者123 更新时间:2023-12-05 00:09:13 25 4
gpt4 key购买 nike

我想编写一个程序来从 .NET 程序集中查找所有 com 可见的 .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));
}
}
}

关于.net - 从 .NET 程序集获取 ProgID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/738720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com