问题描述
我有以下枚举:
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.
我发现了以下针对此问题的解决方案():
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.
推荐答案
尝试模式。
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;
}
}
更新
显式(或隐式)类型转换可以通过
UpdateExplicit (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,请确保将字典字段放在类中的枚举成员字段之前。这是因为静态字段初始化程序是按声明顺序调用的,并且是在静态构造函数之前调用的,这造成了一种奇怪且必要的但令人困惑的情况,即可以在初始化所有静态字段之前以及在调用静态构造函数之前调用实例构造函数。 / li>
- n.b. In order that the initialisation of the the "enum member" fields doesn't throw a NullReferenceException when calling the instance constructor, be sure to put the Dictionary field before the "enum member" fields in your class. This is because static field initialisers are called in declaration order, and before the static constructor, creating the weird and necessary but confusing situation that the instance constructor can be called before all static fields have been initialised, and before the static constructor is called.
在实例构造函数中填充此映射
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(); }
这篇关于枚举的字符串表示形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!