枚举的字符串表示 [英] String representation of an Enum
问题描述
我有以下枚举:
public enum AuthenticationMethod
{
FORMS = 1,
WINDOWSAUTHENTICATION = 2,
SINGLESIGNON = 3
}
然而,问题是当我要求 AuthenticationMethod.FORMS 而不是 id 1 时,我需要FORMS"这个词.
The problem however is that I need the word "FORMS" when I ask for AuthenticationMethod.FORMS and not the id 1.
我找到了针对此问题的以下解决方案(link):
I have found the following solution for this problem (link):
首先,我需要创建一个名为StringValue"的自定义属性:
First I need to create a custom attribute called "StringValue":
public class StringValue : System.Attribute
{
private readonly string _value;
public StringValue(string value)
{
_value = value;
}
public string Value
{
get { return _value; }
}
}
然后我可以将此属性添加到我的枚举器中:
Then I can add this attribute to my enumerator:
public enum AuthenticationMethod
{
[StringValue("FORMS")]
FORMS = 1,
[StringValue("WINDOWS")]
WINDOWSAUTHENTICATION = 2,
[StringValue("SSO")]
SINGLESIGNON = 3
}
当然,我需要一些东西来检索该 StringValue:
And of course I need something to retrieve that StringValue:
public static class StringEnum
{
public static string GetStringValue(Enum value)
{
string output = null;
Type type = value.GetType();
//Check first in our cached results...
//Look for our 'StringValueAttribute'
//in the field's custom attributes
FieldInfo fi = type.GetField(value.ToString());
StringValue[] attrs =
fi.GetCustomAttributes(typeof(StringValue),
false) as StringValue[];
if (attrs.Length > 0)
{
output = attrs[0].Value;
}
return output;
}
}
现在我已经有了为枚举器获取字符串值的工具.然后我可以像这样使用它:
Good now I've got the tools to get a string value for an enumerator. I can then use it like this:
string valueOfAuthenticationMethod = StringEnum.GetStringValue(AuthenticationMethod.FORMS);
好的,现在所有这些都像一个魅力,但我发现它需要大量的工作.我想知道是否有更好的解决方案.
Okay now all of these work like a charm but I find it a whole lot of work. I was wondering if there is a better solution for this.
我也尝试过使用字典和静态属性的东西,但也没有更好.
I also tried something with a dictionary and static properties but that wasn't better either.
推荐答案
尝试 type-safe-enum 模式.
public sealed class AuthenticationMethod {
private readonly String name;
private readonly int value;
public static readonly AuthenticationMethod FORMS = new AuthenticationMethod (1, "FORMS");
public static readonly AuthenticationMethod WINDOWSAUTHENTICATION = new AuthenticationMethod (2, "WINDOWS");
public static readonly AuthenticationMethod SINGLESIGNON = new AuthenticationMethod (3, "SSN");
private AuthenticationMethod(int value, String name){
this.name = name;
this.value = value;
}
public override String ToString(){
return name;
}
}
<小时>
更新显式(或隐式)类型转换可以通过
Update Explicit (or implicit) type conversion can be done by
使用映射添加静态字段
adding static field with mapping
private static readonly Dictionary<string, AuthenticationMethod> instance = new Dictionary<string,AuthenticationMethod>();
- n.b.为了在调用实例构造函数时枚举成员"字段的初始化不会抛出 NullReferenceException,请确保将 Dictionary 字段放在类中的枚举成员"字段之前.这是因为静态字段初始化程序是按声明顺序在静态构造函数之前调用的,造成了奇怪且必要但令人困惑的情况,即可以在初始化所有静态字段之前和调用静态构造函数之前调用实例构造函数.
在实例构造函数中填充这个映射
filling this mapping in instance constructor
instance[name] = this;
并添加用户定义的类型转换运算符
public static explicit operator AuthenticationMethod(string str)
{
AuthenticationMethod result;
if (instance.TryGetValue(str, out result))
return result;
else
throw new InvalidCastException();
}
这篇关于枚举的字符串表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!