Can I make only certain C# classes COM-exposed?
See the question and my original answer on StackOverflowI can declare the whole assembly to be invisible to COM, like this (in fact when you use Visual Studio C# class library template it should put it itself in AssemblyInfo.cs):
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
Now, in each class I can decide it will be visible to COM or not like here:
using System;
using System.Runtime.InteropServices;
namespace ClassLibrary1
{
[ProgId("MyCoolClass")]
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class MyCoolVisibleClass
{
public void SayHello()
{
Console.WriteLine("Hello COM world");
}
// explicit non COM visible because it's set to true at class level
[ComVisible(false)]
public void SayHello2()
{
Console.WriteLine("Hello world");
}
}
// implicit non COM visible
public class MyCoolInvisibleClass
{
public void SayHello()
{
Console.WriteLine("Hello world");
}
}
}
You can use the project properties to register ("Register for COM Interop" checkbox) , but I personally register myself with a command line like this (for 64-bit registry world):
%windir%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe ClassLibrary1.dll /codebase /tlb
This outputs something like this:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe ClassLibrary1.dll /codebase /tlb
Microsoft .NET Framework Assembly Registration Utility version 4.8.3752.0
for Microsoft .NET Framework version 4.8.3752.0
Copyright (C) Microsoft Corporation. All rights reserved.
RegAsm : warning RA0000 : Registering an unsigned assembly with /codebase can cause your assembly to interfere with other applications that may be installed on the same computer. The /codebase switch is intended to be used only with signed assemblies. Please give your assembly a strong name and re-register it.
Types registered successfully
Assembly exported to 'D:\KilroyWasHere\ClassLibrary1.tlb', and the type library was registered successfully
And I can check what's really inside the .tlb using OleView from Windows SDK: