我可以只公开某些 C# 类 COM 吗?
Can I make only certain C# classes COM-exposed?
我有一个 C# 库项目,该项目旨在通过 COM 从非托管 C++ 代码中使用。
只有 2 或 3 个方法将以这种方式被调用,但我收到这样的警告:
warning : Type library exporter warning processing ''.
Warning: Type library exporter encountered a generic type instance in
a signature. Generic code may not be exported to COM.
这些属性和方法不是为从 C++ 访问而设计的,事实上它们甚至不是 public
方法,因此它们(肯定)无论如何都不会可见。
真的有两个问题:
- Q1:如何控制导出的内容? classes/methods 或其他什么的访问修饰符?
- 问题 2:如何查看导出的内容,例如检查类型库中的内容,看看我是否遗漏了什么
最好仔细检查一下,我没有用大量不应该存在的东西使我的类型库膨胀...
我可以像这样声明整个程序集对 COM 不可见(实际上,当您使用 Visual Studio C# class 库模板时,它应该将它自己放在 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)]
现在,在每个 class 中,我可以决定它是否对 COM 可见,如下所示:
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");
}
}
}
您可以使用项目属性来注册("Register for COM Interop" 复选框),但我个人使用这样的命令行注册自己(对于 64 位注册表世界):
%windir%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe ClassLibrary1.dll /codebase /tlb
输出如下:
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
我可以使用 OleView from Windows SDK:
检查 .tlb 中的真实内容
我有一个 C# 库项目,该项目旨在通过 COM 从非托管 C++ 代码中使用。
只有 2 或 3 个方法将以这种方式被调用,但我收到这样的警告:
warning : Type library exporter warning processing ''. Warning: Type library exporter encountered a generic type instance in a signature. Generic code may not be exported to COM.
这些属性和方法不是为从 C++ 访问而设计的,事实上它们甚至不是 public
方法,因此它们(肯定)无论如何都不会可见。
真的有两个问题:
- Q1:如何控制导出的内容? classes/methods 或其他什么的访问修饰符?
- 问题 2:如何查看导出的内容,例如检查类型库中的内容,看看我是否遗漏了什么
最好仔细检查一下,我没有用大量不应该存在的东西使我的类型库膨胀...
我可以像这样声明整个程序集对 COM 不可见(实际上,当您使用 Visual Studio C# class 库模板时,它应该将它自己放在 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)]
现在,在每个 class 中,我可以决定它是否对 COM 可见,如下所示:
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");
}
}
}
您可以使用项目属性来注册("Register for COM Interop" 复选框),但我个人使用这样的命令行注册自己(对于 64 位注册表世界):
%windir%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe ClassLibrary1.dll /codebase /tlb
输出如下:
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
我可以使用 OleView from Windows SDK:
检查 .tlb 中的真实内容