How to fix 'NonComVisibleBaseClass was detected'

By FoxLearn Published on Feb 15, 2024   612
The error "NonComVisibleBaseClass was detected" in Visual Studio typically occurs when you are working with COM interop in .NET, and your class inherits from a base class that isn't COM-visible.

This can cause issues if you're trying to expose your class to COM, but the base class is not decorated with the appropriate COM visibility attributes.

Mark the Base Class as COM Visible

If you have control over the base class, you can mark it as COM-visible. Add the [ComVisible(true)] attribute to the base class.

[ComVisible(true)]
public class BaseClass
{
    // Your base class code
}

Mark the Derived Class as COM Visible

Even if the base class is not COM visible, you can still make the derived class COM-visible. Make sure the class you're trying to expose to COM is marked with the [ComVisible(true)] attribute.

[ComVisible(true)]
public class MyClass : BaseClass
{
    // Your class code
}

If you don’t have access to the base class, you can still make your derived class COM-visible.

Go to your project's properties and enable COM interop:

  • In Visual Studio, go to Debug > Windows > Exception Settings.
  • In the Exception Settings window, expand the Managed Debugging Assistants section.
  • Find and uncheck NonComVisibleBaseClass.

For older versions of Visual Studio:

  1. Navigate to Debug > Exceptions.
  2. Expand the Managed Debugging Assistants section.
  3. Uncheck the NonComVisibleBaseClass option.
  4. Click OK to save your changes.