Search code examples
.netdllcomvisible

How can I tell if a DLL is ComVisible?


I have a DLL that may or may not have its ComVisible attribute set to true. I'm not sure how it was built, or with what attributes? All I know is that it's a .Net DLL. Simply put, how can I tell if it is Com Visible?

Sorry if this is a duplicate. All of my searches about this return results that show how to make a DLL ComVisible. I know how to do that.


Solution

  • You could check the ComVisibleAttribute of the assembly using reflection:

    private static bool IsComVisible(string assemblyPath)
    {
      var assembly = Assembly.LoadFile(assemblyPath);
    
      var attributes = assembly.GetCustomAttributes(typeof(ComVisibleAttribute), false);
    
      if (attributes.Length > 0)
      {
        return ((ComVisibleAttribute)attributes[0]).Value;
      }
    
      return false;
    }