如何在C ++/CLI中捕获C#枚举 [英] How do I trap a C# enum in C++/CLI

查看:111
本文介绍了如何在C ++/CLI中捕获C#枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组由XSD.exe生成的C#类.
我很难从该枚举中获取价值:

I have a set of C# classes produced by XSD.exe
I am having difficulty geting the value from this enum:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://emcs.dgtaxud.ec/v10/tcl")]
public enum OriginTypeCode
{
    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("1")]
    Item1,
    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("2")]
    Item2,
}



我的代码是用C ++/CLI编写的.

当我想要"1"时,我只能提取"Item1".



My code is written in C++/CLI.

All I can extract is "Item1" when I want "1"

推荐答案

可能您正在用其基本整数值弄乱枚举成员的名称.如果使用此属性,请在此处查看用法示例: http://msdn.microsoft .com/en-us/library/system.xml.serialization.xmlenumattribute.aspx [
Probably you''re messing up the names of enumeration members with their underlying integer values. Look at the sample of usage if this attribute here: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlenumattribute.aspx[^].

The serialization mechanism for enumeration is based of enumeration member identifiers, not underlying integer values. The values or the XmlEnum attribute should be valid and unique member identifiers, but "1", "2" are not. To assign integer values you need you should do something like this:

public enum OriginTypeCode
{
    Item1 = 1,
    Item2 = 2,
}



另外,C ++/CLI与C#之间没有界限.如果在用任何一种语言编写的程序集中定义任何枚举类型,并在用任何一种语言编写的另一个程序集中引用该程序集,则声明将以完全相同的方式解释,包括那些基础整数值.

当使用数据的序列化表示时,用一种语言编写的文件/流将被用另一种语言编写的程序集正确读取,因为它们共享相同的枚举类型声明,并且枚举成员由他们的名字.这是一个对于版本之间的兼容性很重要的功能:如果在枚举类型中添加/插入其他枚举成员,则旧数据中的现有成员将由新版本的软件固件在语义上进行解释,这通常是您想要的. >
枚举成员及其各自的基础整数值之间的映射实际上是非常微妙的问题,并且是一些细微问题的源头(但也有一些特征).要理解这一点,请阅读我关于以下主题的文章:
枚举类型不枚举!解决.NET和语言限制 [ ^ ].我会详细解释.

在您的情况下,无需使用System.Xml.Serialization.XmlEnumAttribute. (顺便说一句,建议的做法是在属性应用程序的位置使用缩写名称:[XmlEnum]而不是[XmlEnumAttribute].)此属性的用途之一是在枚举成员使用时为其赋予更多描述性名称在源代码中无法做到;另一个用途是使用序列化与无法修改的遗留系统集成.

—SA



Also, there is no boundary between C++/CLI and C#. If you define any enumeration type in an assembly written in any of these languages and reference this assembly in another assembly written in any of these language, the declarations will be interpreted in exact same way, including those underlying integer values.

When you use serialized presentation of your data, the file/stream written using by one of the languages, will be correctly read by the assembly written in a different language, because they share the same enumeration type declaration and because the enumeration members are identified by their names. This is an feature important for compatibility between versions: if you add/insert additional enumeration member in your enumeration types, existing members from your legacy data will be interpreted by new version of your sofrware semantically, which is usually what you want.

The mapping between enumeration members and their respective underlying integer values is actually a very delicate matter and a source of some subtle problems (but features, too). To understand this, please read my article on the topic: Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^]. I explain it in detail.

There is no need to use System.Xml.Serialization.XmlEnumAttribute in your case. (By the way, recommended practice is using abbreviated name at the point of attribute application: [XmlEnum] instead of [XmlEnumAttribute].) One of the uses of this attribute it to give enumeration members more descriptive names when it it not possible to do it in the source code; another use is using serialization for integration with a legacy system which cannot be modified.

—SA


这篇关于如何在C ++/CLI中捕获C#枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆