ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – 如何检查程序集中的类型是否为ComVisible

2019-05-28 12:54:34  阅读:321  来源: 互联网

标签:c net interop com com-interop


我正在为Enterprise Architect编写一个加载项管理器.

当加载程序集(addin dll)时,我需要知道程序集中定义的哪些类型是COM可见的,所以我可以添加所需的注册表项来为COM interop注册它.

这是我到目前为止所拥有的:

public EAAddin(string fileName):base()
{
    //load the dll
    this.addinDLL = Assembly.LoadFrom(fileName);
    //register the COM visible classes
    this.registerClasses(this.addinDLL);
    //load referenced dll's
    foreach (AssemblyName reference in this.addinDLL.GetReferencedAssemblies())
    {
          if (System.IO.File.Exists(
                 System.IO.Path.GetDirectoryName(this.addinDLL.Location) + 
                    @"\" + reference.Name + ".dll"))
          {
            Assembly referencedAssembly = System.Reflection.Assembly.LoadFrom(System.IO.Path.GetDirectoryName(this.addinDLL.Location) + @"\" + reference.Name + ".dll");
            //register the COM visible classes for the registered assembly
            this.registerClasses(referencedAssembly);
// ... more code ...
private void registerClasses (Assembly assembly)
{
    foreach (Type type in assembly.GetExportedTypes()) 
    {
            register(type, assembly);
    }
}

private void register(Type type, Assembly assembly)
{
    var attributes = type.GetCustomAttributes(typeof(ComVisibleAttribute),false);
    if  (attributes.Length > 0)
    {
        ComVisibleAttribute comvisible = (ComVisibleAttribute)attributes[0];
        if (comvisible.Value == true)
        {
            //TODO add registry keys here
        }
    }            
}

这不起作用,因为类型似乎不包含ComVisibleAttribute.

任何人都知道如何弄清楚程序集中的哪些ExportedTypes是COM可见的?

解决方法:

感谢Paulo的评论我能够弄明白.
如果类型与Assembly默认值不同,则该类型仅具有ComVisibleAttribute.

所以这个操作(调用其中一个类型返回Assembly.GetExportedTypes())似乎可以解决问题

/// <summary>
/// Check if the given type is ComVisible
/// </summary>
/// <param name="type">the type to check</param>
/// <returns>whether or not the given type is ComVisible</returns>
private bool isComVisible(Type type)
{
    bool comVisible = true;
    //first check if the type has ComVisible defined for itself
    var typeAttributes = type.GetCustomAttributes(typeof(ComVisibleAttribute),false);
    if  (typeAttributes.Length > 0)
    {
         comVisible = ((ComVisibleAttribute)typeAttributes[0]).Value;
    }
    else
    {
        //no specific ComVisible attribute defined, return the default for the assembly
        var assemblyAttributes = type.Assembly.GetCustomAttributes(typeof(ComVisibleAttribute),false);
        if  (assemblyAttributes.Length > 0)
        {
             comVisible = ((ComVisibleAttribute)assemblyAttributes[0]).Value;
        }
    }
    return comVisible;
}   

标签:c,net,interop,com,com-interop
来源: https://codeday.me/bug/20190528/1171130.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有