如何使用枚举属性思考?

时间:2011-07-12 13:29:41

标签: c# .net reflection

我打开你的想法:我真的不喜欢这种用法。如何通过属性将枚举转换为字符串。但请注意我的问题StringValueAttribute还有多个构造函数字段。如何通过StringValueAttribute开发GetStringValue Extention方法???最诚挚的问候......



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

namespace UsingAttributes
{
        static void Main(string[] args)
        {
            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 i = 0;
                foreach (PropertyInfo item in pi)
                {
                   // Vals[i] =  How to return String[] 

                }
            // i dislike return values one by one : attribs[0].UserName 
           //return attribs.Length > 0 ? attribs[0].UserName : null; // i have more values
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我不得不说这是一种奇怪的方法,但您只需要将代码更改为:

static void Main(string[] args)
        {
            object[] Objects = UserType.Login.GetStringValue();
            for (int x = 0; x < Objects.Length; ++x)
                Console.WriteLine(Objects[x].ToString());
            Console.Read();
        }
    }

    public static class Extentions
    {
        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 the first if there was a match.

            object[] Vals = new object[3];
            Vals[0] = attribs[0].UserName;
            Vals[1] = attribs[0].PassWord;
            Vals[2] = attribs[0].Something;
            return Vals;
        }
    }

答案 1 :(得分:0)

首先请注意,decimal 不能用作属性参数。您可以使用double,也可以使用字符串,然后将其转换为小数。

如果替换

,则GetStringValue()应该有效
    // Vals[i] =  How to return String[]

    Object val = item.GetValue(attribs[0], null);
    Vals[i++] = val != null ? val.ToString() : "";

如果您使用的是C#4.0,则不需要三个构造函数。您可以使用可选参数:

public StringValueAttribute(string Username, string Password = "nothing", double something = 0)         
{             
     ...
}         

在旧版本的C#中,您可以使用OptionalAttribute

public StringValueAttribute(string Username, [Optional] string Password, [Optional] double something)         
{             
     ...
}