如何开发枚举字符串解析扩展方法?

时间:2011-07-31 15:44:58

标签: c# visual-studio reflection

我有一个很大的问题,我尝试将parse enum int值开发为字符串值。我的参考语言是“http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx”,但我有多个属性2或3或4或45或100000000如何解析它?我在下面编写了一些代码:但是null引用异常: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 个答案:

答案 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();

}

请注意,您要返回一个Objects数组,而不是字符串,因为在您的属性中,您拥有各种类型的属性。