2012-05-02 32 views
16

Tôi đã đoạn mã sauSử dụng EnumMemberAttribute và làm chuyển đổi chuỗi tự động

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

Tôi muốn làm như sau:

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

tôi đã thực hiện phần thứ hai sử dụng DataContractSerializer (xem mã bên dưới), nhưng có vẻ như rất nhiều công việc.

Tôi có thiếu thứ gì đó hiển nhiên không? Ý tưởng? Cảm ơn.

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; 
     } 
    } 
+1

Tôi đã như tùy chọn này vì nó mở rộng Enum: http: // stackoverflow. com/a/4367868/1243316 –

Trả lời

26

Đây là đề xuất của tôi - nó sẽ cho bạn những ý tưởng về làm thế nào để làm điều này (kiểm tra cũng 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); 
} 
3

Bạn có thể sử dụng phản ánh để nhận giá trị EnumMemberAttribute.

public static string ToEnumString<T>(T instance) 
{ 
    if (!typeof(T).IsEnum) 
     throw new ArgumentException("instance", "Must be enum type"); 
    string enumString = instance.ToString(); 
    var field = typeof(T).GetField(enumString); 
    if (field != null) // instance can be a number that was cast to T, instead of a named value, or could be a combination of flags instead of a single value 
    { 
     var attr = (EnumMemberAttribute)field.GetCustomAttributes(typeof(EnumMemberAttribute), false).SingleOrDefault(); 
     if (attr != null) // if there's no EnumMember attr, use the default value 
      enumString = attr.Value; 
    } 
    return enumString; 
} 

Tùy thuộc vào cách hoạt động của ToEnum, bạn cũng có thể muốn sử dụng cách tiếp cận này ở đó. Ngoài ra, loại có thể được suy ra khi gọi ToEnumString, ví dụ: SerializerHelper.ToEnumString(status);

Các vấn đề liên quan