将 enum 序列化为 int
我希望通过网络服务返回以下类,其中包括一个枚举类型作为其成员之一。
[Serializable, XmlRoot("GeoCoordinate")]
public class GeoCoordinate
{
public enum AccuracyLevel
{
Unknown = 0,
Country = 1,
Region = 2,
SubRegion = 3,
Town = 4,
PostalCode = 5,
Street = 6,
Intersection = 7,
Address = 8,
Premise = 9
}
private AccuracyLevel _accuracy;
// ... more members
public AccuracyLevel Accuracy
{
get { return _accuracy; }
set { _accuracy = value;}
}
}
这可以正常工作,但会返回以下形式的结果:
<!-- ... -->
<Accuracy>Unknown or Country or Region or SubRegion or Town or
PostalCode or Street or Intersection or Address or Premise</Accuracy>
<!-- ... -->
我希望它简单地返回一个整数,而不是表示枚举的字符串。可以在不更改 GeoCooperative.Accuracy
类型的情况下完成此操作吗?
I am looking to return the following class through a web service, which includes an enum type as one of its members.
[Serializable, XmlRoot("GeoCoordinate")]
public class GeoCoordinate
{
public enum AccuracyLevel
{
Unknown = 0,
Country = 1,
Region = 2,
SubRegion = 3,
Town = 4,
PostalCode = 5,
Street = 6,
Intersection = 7,
Address = 8,
Premise = 9
}
private AccuracyLevel _accuracy;
// ... more members
public AccuracyLevel Accuracy
{
get { return _accuracy; }
set { _accuracy = value;}
}
}
This works correctly, but will return a result in the form of:
<!-- ... -->
<Accuracy>Unknown or Country or Region or SubRegion or Town or
PostalCode or Street or Intersection or Address or Premise</Accuracy>
<!-- ... -->
Instead of a string that represents the enum, I would like it to simply return an integer. Can this be done without changing the type of GeoCoordinate.Accuracy
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然这是一个黑客,但我认为使用
在这种情况下,每个枚举成员上的 XmlEnumAttribute
是最容易接受的。如果此枚举更大,则最好在 Accuracy 属性上使用XmlIgnore
,并向该类添加一个额外的 int 属性,如该问题的另一个答案中所述。Usng
XmlEnumAttribute
意味着只需要修改 enum 本身,并且无论在哪里使用它,xml 都会像 int 一样序列化。Although it is a hack, I deemed using
XmlEnumAttribute
on each of the enum members to be most pallatable in this case. If this enum were much larger, it would probably be better to useXmlIgnore
on the Accuracy property, and add an additional int property to the class as described in another answer to this question.Usng
XmlEnumAttribute
means that only the enum itself needs to be modified, and will xml serialize like an int wherever it is used.我相信您需要在枚举上使用
[XmlIgnore]
,并创建第二个返回整数值的属性:请注意, XML 序列化器。
另请注意,
AccuracyLevelInt
属性可能未正确实现。我现在正在调查此事。I believe you'll need to use
[XmlIgnore]
on the enum, and create a second property which returns the integer value:Note that
[Serializable]
is not used by the XML Serializer.Also, note that the
AccuracyLevelInt
property is probably implemented incorrectly. I'm looking into that now.使用
[Serialized]
或[DataContract]
修饰枚举。这两个属性之间存在一些差异,请务必检查一下(这篇博文 可能会对此有所帮助)。并使用[EnumMember]
标记各个枚举项。我从未检查过枚举在传输过程中是什么样子,但这样做将确保它到达另一端,并且如果您生成客户端代理,还将确保它被拾取。Decorate the enum with
[Serializable]
or[DataContract]
. There are some differences between the two attributes, make sure you check it out (this blog post may help with that). And mark the individual enum items with[EnumMember]
. I have never checked what the enum looks like in transit, but doing this will ensure that it arrives at the other end, and will also ensure that it gets picked up if you generate a client side proxy.