Enum not serializing
Asked Answered
I

4

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?

Italianize answered 14/5, 2012 at 19:3 Comment(4)
Did you also mark the property you want transfer with DataMember?Demonolater
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.Vertebrate
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 enumsItalianize
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?Samiel
I
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

Italianize answered 14/5, 2012 at 23:52 Comment(2)
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.Retrorocket
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.Italianize
M
4

Try this.

[Serializable]
public enum EnumToSerialize
{
    [XmlEnum("1")]
    One = 1,
    [XmlEnum("2")]
    Two = 2
}
Mechanic answered 14/5, 2012 at 19:7 Comment(1)
Tried that, still no go. When I serialize it/put the message on the MSMQ, it does not even serialize that propertyItalianize
I
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

Italianize answered 14/5, 2012 at 23:52 Comment(2)
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.Retrorocket
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.Italianize
S
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.

Stradivarius answered 14/5, 2012 at 23:23 Comment(4)
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.Italianize
Yes. The sample shows that the properties have to be public. Happy coding!Stradivarius
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....Italianize
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;Stradivarius
W
0

I use this, which works for a public enum:

[Serializable]
public enum EnumToSerialize
{
    [EnumMember]
    One = 1,
    [EnumMember]
    Two = 2
}
Wilie answered 20/3, 2015 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.