C#字符串的枚举 [英] C# String enums

查看:97
本文介绍了C#字符串的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下枚举:

public enum AuthenticationMethod
{
    FORMS = 1,
    WINDOWSAUTHENTICATION = 2,
    SINGLESIGNON = 3
}

问题不过是,我需要这个词的形式,当我问AuthenticationMethod.FORMS而不是ID 1。

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;
    }

}


更新
显式(或隐式)类型转换可以通过


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,一定要把字典前场在你的类中的枚举成员字段。这是因为静态字段initialisers被称为声明顺序和静态构造函数之前,创建怪异的和必要的,但混乱的局面,所有的静态字段都被初始化前的实例构造函数可以调用,静态构造函数被调用之前。

  • 在实例构造填补这一映射

    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();
    }
    


  • 这篇关于C#字符串的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆