是否可以使用属性指定 COM 枚举字段名称?
我正在用 .net 程序集替换(部分)Delphi COM 库。我已将许多 COM 接口和枚举移至 .net 程序集。一切都很好,除了使用 {enum_type_name}_{enum_value_name} 命名约定将枚举导出到类型库。
示例
[ComVisible(true)]
[Guid("318f9e87-444c-11d5-aed1-00105a758da4")]
public enum KeyIDEnum
{
keLeft = 0,
keCentre = 1,
keRight = 2
}
变成
[
uuid(318F9E87-444C-11D5-AED1-00105A758DA4),
version(1.0)
]
typedef enum tagKeyIDEnum
{
KeyIDEnum_keLeft = 0,
KeyIDEnum_keCentre = 1,
KeyIDEnum_keRight = 2
} KeyIDEnum;
我想知道是否有任何方法可以覆盖此默认行为并指定枚举值名称(可能使用属性)。就像:
[ComVisible(true)]
[Guid("318f9e87-444c-11d5-aed1-00105a758da4")]
public enum KeyIDEnum
{
[Name("keLeft")]
keLeft = 0,
[Name("keCentre")]
keCentre = 1,
[Name("keRight")]
keRight = 2
}
我几乎可以肯定有一种方法可以做到这一点,我只需要知道要使用哪个属性类。
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-aed1-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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
总之,我相信这是可以做到的,但并不容易。我几乎可以肯定属性不会起到作用,因为导出的 MSDN 页面 类型转换讨论了前缀,但没有提及相关属性(而页面的其他部分则提及)。 System.Runtime.InteropServices 中的类列表 ( COM 相关属性所在的地方)也没有任何看起来有希望的属性。
最后,
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.)