代码之家  ›  专栏  ›  技术社区  ›  Espo

如何将枚举值序列化为int?

  •  70
  • Espo  · 技术社区  · 16 年前

    我想将枚举值序列化为int,但我只得到名称。

    这是我的(示例)类和枚举:

    public class Request {
        public RequestType request;
    }
    
    public enum RequestType
    {
        Booking = 1,
        Confirmation = 2,
        PreBooking = 4,
        PreBookingConfirmation = 5,
        BookingStatus = 6
    }
    

    Request req = new Request();
    req.request = RequestType.Confirmation;
    XmlSerializer xml = new XmlSerializer(req.GetType());
    StringWriter writer = new StringWriter();
    xml.Serialize(writer, req);
    textBox1.Text = writer.ToString();
    

    This answer (对于另一个问题)似乎表明枚举应该作为默认值序列化为int,但它似乎没有这样做。以下是我的输出:

    <?xml version="1.0" encoding="utf-16"?>
    <Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <request>Confirmation</request>
    </Request>
    

    6 回复  |  直到 7 年前
        1
  •  149
  •   miha    15 年前

    最简单的方法是使用[XmlEnum]属性,如下所示:

    [Serializable]
    public enum EnumToSerialize
    {
        [XmlEnum("1")]
        One = 1,
        [XmlEnum("2")]
        Two = 2
    }
    

    这将序列化为XML(假设父类是CustomClass),如下所示:

    <CustomClass>
      <EnumValue>2</EnumValue>
    </CustomClass>
    
        2
  •  72
  •   Marc Gravell    16 年前

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

    IXmlSerializable

        3
  •  13
  •   Peter McG    16 年前

    有关使用DataContractSerializer实现所需功能的有趣方法,请参阅下面的完整示例控制台应用程序:

    using System;
    using System.IO;
    using System.Runtime.Serialization;
    
    namespace ConsoleApplication1
    {
        [DataContract(Namespace="petermcg.wordpress.com")]
        public class Request
        {
            [DataMember(EmitDefaultValue = false)]
            public RequestType request;
        }
    
        [DataContract(Namespace = "petermcg.wordpress.com")]
        public enum RequestType
        {
            [EnumMember(Value = "1")]
            Booking = 1,
            [EnumMember(Value = "2")]
            Confirmation = 2,
            [EnumMember(Value = "4")]
            PreBooking = 4,
            [EnumMember(Value = "5")]
            PreBookingConfirmation = 5,
            [EnumMember(Value = "6")]
            BookingStatus = 6
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(Request));
    
                // Create Request object
                Request req = new Request();
                req.request = RequestType.Confirmation;
    
                // Serialize to File
                using (FileStream fileStream = new FileStream("request.txt", FileMode.Create))
                {
                    serializer.WriteObject(fileStream, req);
                }
    
                // Reset for testing
                req = null;
    
                // Deserialize from File
                using (FileStream fileStream = new FileStream("request.txt", FileMode.Open))
                {
                    req = serializer.ReadObject(fileStream) as Request;
                }
    
                // Writes True
                Console.WriteLine(req.request == RequestType.Confirmation);
            }
        }
    }
    

    调用WriteObject后,request.txt的内容如下:

    <Request xmlns="petermcg.wordpress.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <request>2</request>
    </Request>
    

    您需要对DataContractSerializer的System.Runtime.Serialization.dll程序集的引用。

        4
  •  2
  •   Abhishek    7 年前
    using System.Xml.Serialization;
    
    public class Request
    {    
        [XmlIgnore()]
        public RequestType request;
    
        public int RequestTypeValue
        {
          get 
          {
            return (int)request;
          } 
          set
          {
            request=(RequestType)value; 
          }
        }
    }
    
    public enum RequestType
    {
        Booking = 1,
        Confirmation = 2,
        PreBooking = 4,
        PreBookingConfirmation = 5,
        BookingStatus = 6
    }
    

        5
  •  0
  •   Glenn    16 年前

    看看System.Enum类。Parse方法将字符串或int表示形式转换为Enum对象,ToString方法将Enum对象转换为可以序列化的字符串。

        6
  •  0
  •   Adriaan Davel    13 年前

    由于您将显式非顺序值分配给枚举选项,因此我假设您希望能够一次指定多个值(二进制标志),那么接受的答案是您唯一的选项。传入PreBooking | PreBookingConfirmation的整数值为9,序列化程序将无法对其进行反序列化,但使用shim属性强制转换它会很好地工作。或者您可能只是错过了3个值:)