我是开放你的想法:我真的不喜欢这种用法。如何通过属性将枚举转换为字符串。但请小心我的问题StringValueAttribute有不止一个构造函数也字段。如何通过StringValueAttribute开发GetStringValue扩展方法??最好的祝福……
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
}
}
}
我不得不说这是一种奇怪的方式,但您只需要将代码更改为如下内容:
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;
}
}
首先注意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)
{
...
}