简体   繁体   English

C#从整数反序列化枚举

[英]C# deserializing enums from integers

Is it possible to deserialize an enum from an int in c#. 是否可以从c#中的int反序列化枚举。 eg If I have the following class: 例如,如果我有以下课程:

class Employee
{
   public string Name { get; set;}
   public int EmployeeTypeID { get; set;}
}

I can easily create this from XML 我可以轻松地从XML创建它

   <Employee>
       <Name>Joe Bloggs</Name>
       <EmployeeTypeID>1</EmployeeTypeID>
   </Employee>

using something like this: 使用这样的东西:

Employee employee = (Employee)new XmlSerializer(typeof(Employee)).Deserialize(XmlReader);

With very little work involved, this allows me to use one generic service that I can use for all database objects by feeding a select command, connection string and a type in to and retrieve an array of objects without any need for further mapping. 由于涉及的工作非常少,这使我可以使用一个通用服务,通过将select命令,连接字符串和类型输入到并检索对象数组而不需要进一步映射,我可以将其用于所有数据库对象。 However I have come unstuck with enums. 但是我已经忘记了枚举。 Supposing now instead of being an integer EmployeeType is an enum: 假设现在而不是整数EmployeeType是一个枚举:

public enum EmployeeTypeEnum
{
   Admin = 1,
   Sales = 2
}

so my class becomes: 所以我的班级成了:

class Employee
{
   public string Name { get; set;}
   public EmployeeTypeEnum EmployeeTypeID { get; set;}
}

Can I use the same XML and make c# recognise that the int value of EmployeeTypeID in the xml should correspond with the int value of the enum? 我可以使用相同的XML并使c#认识到xml中EmployeeTypeID的int值应该与enum的int值相对应吗? There are other questions similar out there, but none have a very satisfactory answer are quite old, and involve wholesale changes to code. 还有其他类似的问题,但没有一个非常令人满意的答案是相当陈旧的,并涉及批量更改代码。 I am hoping for a better solution... 我希望有更好的解决方案......

As a possible separate note (and slightly in anticipation of some responses), is using enums for this a practise best avoided? 作为一个可能的单独注释(并略微预期一些响应),使用枚举最好避免这种做法? Should I be using Key-Value pairs? 我应该使用键值对吗? I would always use Key-value pairs (or similar) if there were likely to be changes, but in this case EmployeeType is fixed and will never change. 如果可能存在更改,我将始终使用键值对(或类似值),但在这种情况下,EmployeeType是固定的,永远不会更改。

Theoretically (= I haven't tried it), adding the XmlEnum attribute to your enum values should do the trick: 从理论上讲 (=我还没试过),将XmlEnum属性添加到你的枚举值应该可以解决问题:

public enum EmployeeTypeEnum 
{ 
    [XmlEnum("1")] Admin = 1, 
    [XmlEnum("2")] Sales = 2 
} 

This tells XmlSerializer that a value of EmployeeTypeEnum.Admin is to be serialized as the string 1 and vice-versa (which is what you need). 这告诉XmlSerializer将EmployeeTypeEnum.Admin的值序列化为字符串1 ,反之亦然 (这是您需要的)。

Regarding your side note: I don't see using enums here as a problem. 关于你的旁注:我不认为在这里使用枚举是一个问题。 If the values in the database are integers and have a fixed meaning, enums are a good solution and, in addition, serve as a documentation to the database values. 如果数据库中的值是整数并且具有固定含义,则枚举是一个很好的解决方案,此外,它还可用作数据库值的文档。

If you xml is in this format: 如果xml采用以下格式:

<Employee>
  <Name>Shiv</Name>
  <EmployeeTypeID>Sales</EmployeeTypeID>
</Employee>

It will work as is. 它会按原样运作。 If you decorate your enum with the XmlEnum attribute like so: 如果你使用XmlEnum属性装饰你的枚举,如下所示:

    public enum EmployeeTypeEnum
{
    [XmlEnum("1")]
    Admin = 1,
    [XmlEnum("2")]
    Sales = 2
}

Then you can use integer values in your xml file and they will be mapped to the enum automagically. 然后,您可以在xml文件中使用整数值,它们将自动映射到枚举。

Personally, I prefer using enums in cases like this. 就个人而言,我更喜欢在这种情况下使用枚举。 Even if the items in the enum could increase over time, I prefer using enums. 即使枚举中的项目可能会随着时间的推移而增加,我更喜欢使用枚举。 In fact such enums are code generated from the database, so in code you don't work with Ids (much more readable). 实际上这样的枚举是从数据库生成的代码,因此在代码中你不能使用ID(更具可读性)。

In xml file, Use named constant in stead of integer value. 在xml文件中,使用命名常量而不是整数值。 It will work just fine. 它会工作得很好。

<Employee>
       <Name>Joe Bloggs</Name>
       <EmployeeTypeID>Admin</EmployeeTypeID>
</Employee>

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

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