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



我有一个很大的问题,我试图开发解析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. getproperty)。名字,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
        }
    }
}

如果我理解正确的话,您希望通过反射读取属性的所有属性(而不是通过名称访问它们)。如果是这样,您的扩展方法应该变成:

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();
}

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

相关内容

  • 没有找到相关文章

最新更新