Is it possible to specify COM enum field name using an attribute?
I am replacing (part of) a Delphi COM library with a .net assembly. I have moved a number of the COM interfaces and enums to the .net assembly. Everything is fine except that the enums are exported to the type library using a {enum_type_name}_{enum_value_name} naming convention.
Example
[ComVisible(true)]
[Guid("318f9e87-444c-11d5-a开发者_如何学Pythoned1-00105a758da4")]
public enum KeyIDEnum
{
keLeft = 0,
keCentre = 1,
keRight = 2
}
Becomes
[
uuid(318F9E87-444C-11D5-AED1-00105A758DA4),
version(1.0)
]
typedef enum tagKeyIDEnum
{
KeyIDEnum_keLeft = 0,
KeyIDEnum_keCentre = 1,
KeyIDEnum_keRight = 2
} KeyIDEnum;
I would like to know if there is any way to override this default behavior and specify the enum value names, possibly using an attribute. Something like:
[ComVisible(true)]
[Guid("318f9e87-444c-11d5-aed1-00105a758da4")]
public enum KeyIDEnum
{
[Name("keLeft")]
keLeft = 0,
[Name("keCentre")]
keCentre = 1,
[Name("keRight")]
keRight = 2
}
I'm almost certain there is a way to do this I just need to know which attribute class to use.
In short, I believe it can be done but it is not easy. I am almost certain an attribute will not do the trick as the MSDN page on exported type conversion talks about the prefixing but does not mention related attributes (whereas other parts of the page do). The listing of classes in System.Runtime.InteropServices (where COM-related attributes live) also does not have any attributes that appear promising.
Finally, a blog post by Mauricio Rojas describes how to do it by modifying the IDL but it looks ugly. (A few other blogs have similar descriptions.)
精彩评论