C# 如何将枚举与属性反射一起使用?
我对你的想法持开放态度:我真的不喜欢这种用法。如何通过属性将枚举转换为字符串。但是请注意我的问题StringValueAttribute也有多个构造函数字段。如何通过StringValueAttribute开发GetStringValue扩展方法???致以最良好的祝愿C# 如何将枚举与属性反射一起使用?,c#,.net,reflection,C#,.net,Reflection,我对你的想法持开放态度:我真的不喜欢这种用法。如何通过属性将枚举转换为字符串。但是请注意我的问题StringValueAttribute也有多个构造函数字段。如何通过StringValueAttribute开发GetStringValue扩展方法???致以最良好的祝愿 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; n
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
}
}
}
首先请注意,
decimal
不能用作属性参数。您可以使用双精度,也可以使用字符串,然后将其转换为十进制
如果替换
// Vals[i] = How to return String[]
与
如果您使用的是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)
{
...
}
我不得不说,这是一种奇怪的方法,但您只需要将代码更改为以下内容:
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;
}
}
static void Main(字符串[]args)
{
object[]Objects=UserType.Login.GetStringValue();
对于(int x=0;x
但我不知道用户名、密码或其他什么,只需在属性类中添加一个函数即可提供信息。如果不可能,只需添加一些反射调用即可获得值。
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;
}
}