C# 枚举未序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10589500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enum not serializing
提问by Ahmed ilyas
I have a WCF service. it is bound to an MSMQ but that is not the issue here. I can serialize an object which has a base class and an interface implemented in the base class and the concrete class derives from the base class - this works fine.
我有一个 WCF 服务。它绑定到一个 MSMQ 但这不是这里的问题。我可以序列化一个对象,它有一个基类和一个在基类中实现的接口,具体类从基类派生 - 这很好用。
however, when I have an enum in the base class and I set that value, then after it being deserialized/read from the MSMQ, that value is still set to the default value (i.e not the one set manually in code)
但是,当我在基类中有一个枚举并设置该值时,在它被反序列化/从 MSMQ 读取后,该值仍设置为默认值(即不是在代码中手动设置的值)
any ideas whats going on? I even marked the enum as a DataContract and also each of the Enum members with an EnumMember attribute.
有什么想法是怎么回事?我什至将枚举标记为 DataContract,并且每个 Enum 成员都带有 EnumMember 属性。
how can I serialize enums?
如何序列化枚举?
采纳答案by Ahmed ilyas
The property was protected. set it to Public and viola - serialized the enum property. Kinda bad as the property resides in a bass class....rather have it protected
财产受到保护。将其设置为 Public 和 viola - 序列化枚举属性。有点糟糕,因为该物业位于低音类……而是对其进行保护
回答by animaonline
Try this.
尝试这个。
[Serializable]
public enum EnumToSerialize
{
[XmlEnum("1")]
One = 1,
[XmlEnum("2")]
Two = 2
}
回答by Emmie Lewis-Briggman
回答by cederlof
I use this, which works for a public enum:
我使用这个,它适用于公共枚举:
[Serializable]
public enum EnumToSerialize
{
[EnumMember]
One = 1,
[EnumMember]
Two = 2
}

