我有以下枚举:
public enum AuthenticationMethod
{
FORMS = 1,
WINDOWSAUTHENTICATION = 2,
SINGLESIGNON = 3
}
然而,问题是当我要求AuthenticationMethod.FORMS而不是id 1时,我需要“FORMS”这个词。
我找到了以下解决此问题的解决方案(链接):
首先,我需要创建一个名为“StringValue”的自定义属性:
public class StringValue : System.Attribute
{
private readonly string _value;
public StringValue(string value)
{
_value = value;
}
public string Value
{
get { return _value; }
}
}
然后我可以将此属性添加到我的枚举器中:
public enum AuthenticationMethod
{
[StringValue("FORMS")]
FORMS = 1,
[StringValue("WINDOWS")]
WINDOWSAUTHENTICATION = 2,
[StringValue("SSO")]
SINGLESIGNON = 3
}
当然,我需要一些东西来检索该字符串值:
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;
}
}
很好,现在我有了获取枚举器的字符串值的工具。
然后我可以像这样使用它:
string valueOfAuthenticationMethod = StringEnum.GetStringValue(AuthenticationMethod.FORMS);
好吧,现在所有这些工作都像一个魅力,但我发现它有很多工作。我想知道是否有更好的解决方案。
我还尝试了一些字典和静态属性,但也不是更好。
网友回答:
使用方法
Enum.GetName(Type MyEnumType, object enumvariable)
如 (假设是一个定义的枚举)Shipper
Shipper x = Shipper.FederalExpress;
string s = Enum.GetName(typeof(Shipper), x);
枚举类上还有一堆其他静态方法值得研究……
网友回答:
尝试类型安全枚举模式。
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;
}
}
更新
显式(或隐式)类型转换可以通过以下方式完成
private static readonly Dictionary<string, AuthenticationMethod> instance = new Dictionary<string,AuthenticationMethod>();
instance[name] = this;
public static explicit operator AuthenticationMethod(string str)
{
AuthenticationMethod result;
if (instance.TryGetValue(str, out result))
return result;
else
throw new InvalidCastException();
}
网友回答:
您可以使用 ToString() 引用名称而不是值
Console.WriteLine("Auth method: {0}", AuthenticationMethod.Forms.ToString());
文档在这里:
http://msdn.microsoft.com/en-us/library/16c1xs4z.aspx
…如果你在Pascal Case中命名你的枚举(就像我一样 – 例如ThisIsMyEnumValue = 1等),那么你可以使用一个非常简单的正则表达式来打印友好的形式:
static string ToFriendlyCase(this string EnumString)
{
return Regex.Replace(EnumString, "(?!^)([A-Z])", " $1");
}
可以轻松地从任何字符串调用:
Console.WriteLine("ConvertMyCrazyPascalCaseSentenceToFriendlyCase".ToFriendlyCase());
输出:
将我疯狂的帕斯卡案例句子转换为友好案例
这样可以节省在房屋周围运行,创建自定义属性并将它们附加到枚举或使用查找表将枚举值与友好字符串相结合,最重要的是它是自我管理的,可以用于任何Pascal Case字符串,这是无限可重用的。当然,它不允许你使用与解决方案提供的枚举不同的友好名称。
不过,对于更复杂的情况,我确实喜欢您的原始解决方案。你可以更进一步,让你的GetStringValue成为枚举的扩展方法,然后你就不需要像StringEnum.GetStringValue那样引用它了。
public static string GetStringValue(this AuthenticationMethod value)
{
string output = null;
Type type = value.GetType();
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;
}
然后,您可以直接从枚举实例轻松访问它:
Console.WriteLine(AuthenticationMethod.SSO.GetStringValue());
模板简介:该模板名称为【在c#枚举的字符串表示形式】,大小是暂无信息,文档格式为.编程语言,推荐使用Sublime/Dreamweaver/HBuilder打开,作品中的图片,文字等数据均可修改,图片请在作品中选中图片替换即可,文字修改直接点击文字修改即可,您也可以新增或修改作品中的内容,该模板来自用户分享,如有侵权行为请联系网站客服处理。欢迎来懒人模板【C#】栏目查找您需要的精美模板。