开发者

C# Partial Class and COM Interface

I'm creating a C# library and am going to expose a COM interface to it.

I understand the steps requried to do this, i.e.

  1. Ensure assumbly GUID is assigned, e.g: [assembly: Guid("dde7717b-2b75-4972-a4eb-b3d040c0a182")]
  2. Ensure COMVibile attribute is True
  3. Put a GUID attrib开发者_如何学Cute on the class, e.g. [GuidAttribute("4df74b15-d531-4217-af7e-56972e393904")]
  4. Register using Regasm.

My question is this. If I have a partial class defined. Do I need to add the GuidAttribute to both these classes?

In fact, thinking about this, I'm guessing this question applies whatever the attribute (e.g. Serializable).

Any help would be appreciated. Thanks.


If you apply an attribute twice to the same class (no matter if in the same file or in two different files) then the class will have the attribute applied twice. A partial class defined in two files is not two classes, it's just one class partially defined in multiple files. So, no, don't repeat the GuidAttribute in each file again.


At compile time, attributes of partial-type definitions are merged. For example, the following declarations:

[System.SerializableAttribute]
partial class Moon { }

[System.ObsoleteAttribute]
partial class Moon { }

are equivalent to:

[System.SerializableAttribute]
[System.ObsoleteAttribute]
class Moon { }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜