Best Practice in Writing a COM-Visible Assembly (C#)

I summarized some best practice rules in writing a COM-visible assembly according to my recent work.

1. In AssemblyInfo.cs, I wrote two attributes:

[assembly: ComVisible(false)]
[assembly: Guid("64729ced-7a8d-4c90-af7f-a41725cfe216")] 

They indicated I didn’t want all public classes to be COM-visible and I didn’t want Visual Studio to implicitly generate a random GUID as my library ID.

2. I unchecked the “Register for COM interop” box in the project’s Build option because I didn’t want Visual Studio to register the assembly on the build machine.

3. I wrote each COM-visible class like the following example:

[ComVisible(true)]
[Guid("7884998B-0504-4CBE-9FF9-6BBAA9776188")]
[ClassInterface(ClassInterfaceType.None)]
public class MyClass
{
}

Here I explicitly declared the class as COM-visible and gave it a GUID as the class ID. Visual Studio would generate a random GUID if I didn’t specify one. I used ClassInterfaceType.None to prevent Visual Studio from generating an interface for the class automatically. The automatic behavior would expose methods which I didn’t want to expose and could introduce unwanted method declarations.

4. I declared an interface for each COM-visible class explicitly like the following example:

[ComVisible(true)]
[Guid("67F6AA4C-A9A5-4682-98F9-15BDF2246A74")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IMyClass
{
}

There was no special rule for InterfaceType. It would depend on application needs. And I only declared the methods which I wanted to expose in the interface.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值