12

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.

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)

any ideas whats going on? I even marked the enum as a DataContract and also each of the Enum members with an EnumMember attribute.

how can I serialize enums?

Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72
  • Did you also mark the property you want transfer with DataMember? – shriek May 14 '12 at 19:39
  • 1
    Disable Just my Code, and enable break on all CLR exceptions, then you should see some internal serialization exception that tells you what is wrong. By default this is never logged anywhere, so it's the only sure way I know to get the true exception that's being hidden. – Richard Anthony Hein May 14 '12 at 19:57
  • Richard: there are NO exceptions at all. I did what you said earlier and nothing....shriek - yes I certainly did. works fine for everything in the class (other objects and value types (i.e ints)) but not enums – Ahmed ilyas May 14 '12 at 23:12
  • can you post your class here please? Can you narrow down to the field which breaks? Is it just a class with 1 enum member? also - what your serialization code? – avs099 May 14 '12 at 23:20

4 Answers4

4

Try this.

[Serializable]
public enum EnumToSerialize
{
    [XmlEnum("1")]
    One = 1,
    [XmlEnum("2")]
    Two = 2
}
animaonline
  • 3,715
  • 5
  • 30
  • 57
  • Tried that, still no go. When I serialize it/put the message on the MSMQ, it does not even serialize that property – Ahmed ilyas May 14 '12 at 23:11
4

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

Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72
  • The definition of the Enum DataContract has to be public for the serializer. However the property being serialized can be any level of protections: private, internal, etc. – ErnieL May 15 '12 at 02:00
  • not according to the tests i have done. if I declare the known type attribute for that TYPE of enum, and set the enum property to protected, then changing that property value to a different enum does not get serialized. if however I change that access level protection to public, only then does it work. – Ahmed ilyas May 16 '12 at 01:25
1

Try this article on MSDN. This example seems to be able to set a property with an enumeration and serialize it. You should be able to get that same value back when de-serializing the object.

  • 1
    Thanks Emmie. Tried that, no go but I just managed to find the problem! I had to make the protected property in the base class to public - and then it serialized it! hmm. – Ahmed ilyas May 14 '12 at 23:51
  • Yes. The sample shows that the properties have to be public. Happy coding! – Emmie Lewis-Briggman May 14 '12 at 23:53
  • 1
    Thanks. What I DO find interesting though, is that if the properties are protected with other data types including DateTime struct, it serializes fine.... but enums appear to be a special case.... – Ahmed ilyas May 15 '12 at 00:21
  • Notice in the example that the enumeration is public but the property that uses the enumeration is internal. I believe that may work. [DataContract] public enum Position { [EnumMember(Value = "Emp")] Employee, [EnumMember(Value = "Mgr")] Manager, [EnumMember(Value = "Ctr")] Contractor, NotASerializableEnumeration } [DataMember] internal Position Description; – Emmie Lewis-Briggman May 15 '12 at 00:30
0

I use this, which works for a public enum:

[Serializable]
public enum EnumToSerialize
{
    [EnumMember]
    One = 1,
    [EnumMember]
    Two = 2
}
cederlof
  • 7,206
  • 4
  • 45
  • 62