2011-07-31 51 views
-2

我有一個很大的問題,我嘗試開發解析枚舉int值爲字符串值。我的參考書目是「http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx」,但我有多個屬性2或3或4或45或100000000如何解析它?我寫了下面的代碼:但空引用異常:Vals [j] = item.GetType()。GetProperty(item.Name,BindingFlags.NonPublic | BindingFlags.Static).ToString();何必解決它?如何開發枚舉字符串解析擴展方法?


using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Reflection; 

namespace EnumString 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      String[] str = UserType.Login.GetStringValue(); 
      //Console.WriteLine(UserType.Login.GetStringValue()); 
      Console.Read(); 
     } 
    } 
    public enum UserType : int 
    { 
     [StringValueAttribute("xyz", "test")] 
     Login = 1 
    } 
    public class StringValueAttribute : Attribute 
    { 
     public string UserName { get; protected set; } 
     public string PassWord { get; protected set; } 
     public decimal Something { get; protected set; } 

     public StringValueAttribute(string Username, string Password, decimal something) 
     { 
      UserName = Username; 
      PassWord = Password; 
      Something = something; 
     } 
     public StringValueAttribute(string Username, string Password) 
     { 
      UserName = Username; 
      PassWord = Password; 
     } 
     public StringValueAttribute(string Username) 
     { 
      UserName = Username; 

     } 

    } 

    public static class Extentions 
    { 
     public static String[] GetStringValue(this Enum value) 
     { 
      // Get the type 
      Type type = value.GetType(); 

      // Get fieldinfo for this type 
      FieldInfo fieldInfo = type.GetField(value.ToString()); 

      // Get the stringvalue attributes 
      StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
       typeof(StringValueAttribute), false) as StringValueAttribute[]; 

      // Return the first if there was a match. 


      PropertyInfo[] pi = attribs[0].GetType().GetProperties(); 
      String[] Vals = new String[pi.Length]; 
      int j = 0; 
      foreach (PropertyInfo item in pi) 
      { 
       Vals[j] = item.GetType().GetProperty(item.Name, BindingFlags.NonPublic | BindingFlags.Static).ToString(); 
       j++; 
      } 
      // i dislike return values one by one : attribs[0].UserName 
      return attribs.Length > 0 ? Vals : null; // i have more values 
     } 
    } 
} 

+1

你的字符串值函數的行爲與調用Enum成員的默認.ToString()方法有什麼不同? – lsuarez

+0

我想他想獲得所有重複值 –

回答

1

如果我理解正確,您想通過反射讀取屬性的所有屬性(而不是通過名稱訪問它們)。 如果是這樣,你的擴展方法應該變成:

public static Object[] GetStringValue(this Enum value) 
{ 
    // Get the type 
    Type type = value.GetType(); 

    // Get fieldinfo for this type 
    FieldInfo fieldInfo = type.GetField(value.ToString()); 

    // Get the stringvalue attributes 
    StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
     typeof(StringValueAttribute), false) as StringValueAttribute[]; 

    PropertyInfo[] pi = attribs[0].GetType().GetProperties(); 
    Object[] Vals = new Object[pi.Length]; 
    int j = 0; 
    foreach (PropertyInfo item in pi) 
    { 
     Vals[j] = item.GetValue(attribs[0],null); 
     j++; 
    } 
    return attribs.Length > 0 ? Vals : null; // i have more values 
} 

或(有位的LINQ):

public static Object[] GetStringValue(this Enum value) 
{ 
    // Get the type 
    Type type = value.GetType(); 

    // Get fieldinfo for this type 
    FieldInfo fieldInfo = type.GetField(value.ToString()); 

    // Get the stringvalue attributes 
    StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
     typeof(StringValueAttribute), false) as StringValueAttribute[]; 

    return attribs[0].GetType().GetProperties() 
     .Select(p => p.GetValue(attribs[0],null)) 
     .ToArray(); 

} 

注意,我們在返回的對象,而不是字符串,數組,因爲在你的屬性你有不同類型的屬性。

-2

通常當我必須做的枚舉字符串我做這樣的事情:

public enum MyEnum { 
    User, 
    Admin, 
    Moderator, 
    Guest 
} 


public static class MyEnumExtensions { 

    public static string GetStringValue(this MyEnum enum) { 

     switch(enum) { 
      case MyEnum.User: 
       return "User"; 
      case MyEnum.Admin: 
       return "Administrator"; 
      case MyEnum.Moderator: 
       return "Moderator"; 
      case MyEnum.Guest: 
       return "Guest"; 
      default: 
       throw new NotImplementedException(); 
     } 
    } 
} 

我明白這不是你的做法,但認爲我把它扔出去那裏作爲一個可能的解決方案。

+0

我認爲主要想法是有一個通用的實現不是嗎? –