使用EnumMemberAttribute并进行自动字符串转换 [英] Using EnumMemberAttribute and doing automatic string conversions

查看:88
本文介绍了使用EnumMemberAttribute并进行自动字符串转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

  [DataContract] 
public enum StatusType
{
[EnumMember(Value =A)]
全部
[EnumMember(Value =I)]
InProcess,
[EnumMember(Value =C ]
完成,
}

我想执行以下操作:

  var s =C; 
StatusType status = SerializerHelper.ToEnum< StatusType>(s); // status is now StatusType.Complete
string newString = SerializerHelper.ToEnumString< StatusType>(status); // newString现在是C

我使用DataContractSerializer完成了第二部分(见下面的代码),但似乎很多工作。我缺少一些明显的东西?想法?感谢。

  public static string ToEnumString&T;(T type)
{
string s;
使用(var ms = new MemoryStream())
{
var ser = new DataContractSerializer(typeof(T));
ser.WriteObject(ms,type);
ms.Position = 0;
var sr = new StreamReader(ms);
s = sr.ReadToEnd();
}
使用(var xml = new XmlTextReader(s,XmlNodeType.Element,null))
{
xml.MoveToContent();
xml.Read();
return xml.Value;
}
}


解决方案

是我的命题 - 它应该给你这样的想法(另请参阅获取枚举的属性值):

  public static string ToEnumString< T>(T type)
{
var enumType = typeof(T);
var name = Enum.GetName(enumType,type);
var enumMemberAttribute =((EnumMemberAttribute [])enumType.GetField(name).GetCustomAttributes(typeof(EnumMemberAttribute),true))。
return enumMemberAttribute.Value;
}

public static T ToEnum< T>(string str)
{
var enumType = typeof(T);
foreach(Enum.GetNames(enumType)中的var名称)
{
var enumMemberAttribute =((EnumMemberAttribute [])enumType.GetField(name).GetCustomAttributes(typeof(EnumMemberAttribute),true) )。单();
if(enumMemberAttribute.Value == str)return(T)Enum.Parse(enumType,name);
}
//抛出异常或任何你想要的处理或
返回默认(T);
}


I have the following code

[DataContract]
public enum StatusType
{
    [EnumMember(Value = "A")]
    All,
    [EnumMember(Value = "I")]
    InProcess,
    [EnumMember(Value = "C")]
    Complete,
}

I'd like to do the following:

 var s = "C";
 StatusType status = SerializerHelper.ToEnum<StatusType>(s);   //status is now StatusType.Complete
 string newString = SerializerHelper.ToEnumString<StatusType>(status);   //newString is now "C"

I've done the second part using DataContractSerializer (see code below), but it seems like a lot of work.

Am I missing something obvious? Ideas? Thanks.

    public static string ToEnumString<T>(T type)
    {
        string s;
        using (var ms = new MemoryStream())
        {
            var ser = new DataContractSerializer(typeof(T));
            ser.WriteObject(ms, type);
            ms.Position = 0;
            var sr = new StreamReader(ms);
            s = sr.ReadToEnd();
        }
        using (var xml = new XmlTextReader(s, XmlNodeType.Element, null))
        {
            xml.MoveToContent();
            xml.Read();
            return xml.Value;
        }
    }

解决方案

Here is my proposition - it should give you the idea on how to do this (check also Getting attributes of Enum's value):

public static string ToEnumString<T>(T type)
{
    var enumType = typeof (T);
    var name = Enum.GetName(enumType, type);
    var enumMemberAttribute = ((EnumMemberAttribute[])enumType.GetField(name).GetCustomAttributes(typeof(EnumMemberAttribute), true)).Single();
    return enumMemberAttribute.Value;
}

public static T ToEnum<T>(string str)
{
    var enumType = typeof(T);
    foreach (var name in Enum.GetNames(enumType))
    {
        var enumMemberAttribute = ((EnumMemberAttribute[])enumType.GetField(name).GetCustomAttributes(typeof(EnumMemberAttribute), true)).Single();
        if (enumMemberAttribute.Value == str) return (T)Enum.Parse(enumType, name);
    }
    //throw exception or whatever handling you want or
    return default(T);
}

这篇关于使用EnumMemberAttribute并进行自动字符串转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆