你可以强制一个枚举值序列化到一个整数? [英] Can you force the serialization of an enum value into an integer?

查看:149
本文介绍了你可以强制一个枚举值序列化到一个整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  如何序列化一个枚举值作为一个int <? / A>

Possible Duplicate:
How do I serialize an enum value as an int?

所有!

我不知道是否有一种方法来强制,而不是它的字符串重新presentation枚举值序列化到其整数值。

I'm wondering if there's a way to force the serialization of an enum value into its integer value, instead of its string representation.

把你的来龙去脉: 我们使用,在很大程度上依赖于Web服务,为我们所有的请求头一个基类,independantly请求的类型的Web应用程序。

To put you into context: We're using, in a web application that heavily relies on web services, a single baseclass for all our request headers, independantly of the type of request.

我要一个结果字段添加到头部,所以我们必须通过提示回调用应用程序,如何操作继续在网络服务端的地方。我们已经宣布该效果的枚举,但由于我们对这些Web服务可能不知道这些枚举调用遗留应用程序,我想送连载这些值作为整数。

I want to add a Result field to the header, so we'll have a place to pass hints back to the calling app as to how the operation went on the web service side. We already have an enum declared to that effect, but since we have legacy apps that call on those web services that may not know about those enums, I'd like to send serialize those values as integers.

我们已经有了使用,以减少这些标题的长度[的XmlElement(的ElementName =弦重presentationOfAttributeName),因为我们偶尔会超过IE的最大URL长度,我想知道是否有一个类似的属性,迫使枚举值序列化到整数。

We've already had to cut down on the length of those headers by using the [XmlElement(ElementName = "string representationOfAttributeName")] because we occasionally exceeded IE maximum url length, and I wondered whether there's a similar Attributes to force the serialization of enum values into integers.

任何人都没有听说过这样的属性?

Anyone ever heard of such an attribute?

与以往一样,感谢您的帮助, 帕斯卡尔

As ever, thanks for the help, Pascal

推荐答案

枚举不序列化到整数。但是,一般情况下,如果你不喜欢你的属性之一被序列化到XML,你可以这样做:

Enums do serialize to ints. But generally, if you don't like the way one of your properties gets serialized to XML, you can just do this:

[XmlIgnore]
public MyThing MyThing { get; set; }

[XmlElement("MyThing")]
[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
public string MyThingForSerialization
{
    get { return //convert MyThing to string; }
    set { MyThing = //convert string to MyThing; }
}

这篇关于你可以强制一个枚举值序列化到一个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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