How do I serialize an enum value as an int?

后端 未结 6 711
长发绾君心
长发绾君心 2020-11-28 05:48

I want to serialize my enum-value as an int, but i only get the name.

Here is my (sample) class and enum:

public class Request {
    public RequestTy         


        
6条回答
  •  孤街浪徒
    2020-11-28 06:26

    Most of the time, people want names, not ints. You could add a shim property for the purpose?

    [XmlIgnore]
    public MyEnum Foo {get;set;}
    
    [XmlElement("Foo")]
    [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
    public int FooInt32 {
        get {return (int)Foo;}
        set {Foo = (MyEnum)value;}
    }
    

    Or you could use IXmlSerializable, but that is lots of work.

提交回复
热议问题