简体   繁体   English

如何在C#中使标志枚举数字化其组合值?

[英]How can you make a flags enum serialize its combination values numerically in C#?

Suppose you have a flags enum in C#: 假设您在C#中有一个flags枚举:

[Flags]
public enum FlagsEnum
{
    One = 0x1,
    Two = 0x2,
    Four = 0x4,
    ...
}

If you run this through standard JSON or XML serialization, FlagsEnum.One will be serialized as the string One , FlagsEnum.Two as Two , etc. If you wanted to change it to have the values serialized as the integers 1 , 2 , etc., a typical solution for enums in general is to use the EnumMember attribute or XmlEnum attribute. 如果您通过标准的JSON或XML序列化运行此, FlagsEnum.One将被序列化为串OneFlagsEnum.TwoTwo等,如果你想改变它具有序列化为整数值12 ,等等。 ,通常,枚举的典型解决方案是使用EnumMember属性或XmlEnum属性。

The problem is that with a flags enum, you're expecting several numerical values to be used that aren't specifically declared. 问题在于,使用标志枚举时,您期望使用未明确声明的多个数值。 Googling around doesn't seem to turn up much a solution for that scenario. 谷歌搜索似乎并没有为这种情况提供很多解决方案。 So how would you get something like this to be serialized to integer values that aren't simply powers of 2, showing things in the serialized form like 23 , 15 , etc.? 所以,你怎么会得到这样的事情被序列化到不是单纯的2的幂的整数值,显示出在序列化形式喜欢的东西2315 ,等等?

EDIT 编辑

Part of what I'm looking for is a way to specify this within the enum itself, without adding any extra values to the enum or properties to other types. 我正在寻找的一部分方法是在枚举本身中指定此方法,而不向枚举添加任何额外的值或向其他类型添加属性。 There is a question in my mind, however, whether that is actually possible (basically speaking). 但是,在我的脑海中有一个问题,即实际上是否可能(基本而言)。

Also the main type of serializer that this is really coming up with at the moment is DataContractSerializer . 目前真正提出的串行器的主要类型是DataContractSerializer However typical, standard JSON stuff would also be applicable to this question, and the goal is to avoid changing out the serializers themselves and using anything customer or off the beaten path. 但是,典型的标准JSON内容也适用于此问题,其目的是避免更改序列化程序本身并避免使用任何客户或超出常规。 (Attributes and stuff would be good, if it can be done that way.) (如果可以这样做的话,属性和东西会很好。)

Serialization out of the box allows to save and restore flaged enum values: 开箱即用的序列化允许保存和还原标记的枚举值:

<foo>Two Four</foo>

but with string values in XML 但具有XML中的字符串值

To save it as int interpretation you can make an int property and serialize it instead 要将其保存为int解释,可以创建一个int属性并将其序列化

[XmlIgnore]
public FlagsEnum foo{get;set;}
public int bar
{
    get{ return (int)foo;}
    set{ foo = (FlagsEnum)value;}
}

In that case your XML will have int values and the object will have full enum data 在这种情况下,您的XML将具有int值,并且该对象将具有完整的枚举数据

for DataContractSerializer and XmlSerializer works an option with IXmlSerializable interface implementation, but it's a bit too excess for a big classes 用于DataContractSerializerXmlSerializer可以与IXmlSerializable接口实现一起使用,但是对于大类来说,这有点多余

public void ReadXml(XmlReader reader)
{
    while (reader.Read())
    {
        if (reader.Name == "foo")
        {
            foo = (FlagsEnum)reader.ReadElementContentAsInt();
        }
    }
}

public void WriteXml(XmlWriter writer)
{
    writer.WriteElementString("foo", ((int)foo).ToString());
}

The simplest way would be just creating a separate int property for serialization. 最简单的方法是仅创建一个单独的int属性进行序列化。

public class DTO
{
    [JsonIgnore]
    public FlagsEnum Prop1 { get; set; }

    public int Prop1Serialize { 
       get { return (int)Prop1; }
       set { Prop1 = (FlagsEnum)value; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM