簡體   English   中英

以與Enum相同的方式將字符串值存儲為常量

[英]Storing string values as constants in the same manner as Enum

我知道有一種方法可以讓enum適用於轉換豐富的字符串類型 - 代碼看起來不太漂亮。

有誰知道有這樣的事情:

    public SOMESTRUCTURE SessionKeys : string
{
    value1 = "value1key",
    value2 = "value2key",
    name = "name"
}

所以稍后在我的代碼中我可以將其稱為:

SessionKeys.value1

這是我提出的最好的。 (我沒有編譯它,所以語法可能會關閉。)

public static class SessionKeys
{
    public const string Value1 = "Value1";
    public const string Value2 = "Value2";
    ...
}

使用C#2.0 - 我認為最好的選擇是使用一個靜態類,其值為常量,如John Fisher所建議的那樣。

如果您可以使用C#3.0,則可以使用標准枚舉和簡單的擴展方法以較少令人反感的方式處理轉換。

在這里看到我的答案:
使用反射獲取類型的靜態字段值

這和John Fisher的答案之間的區別在於你可以將SessionKeys作為函數參數傳遞,並獲得你想要的類似enum的語義。

上一個問題要求VB.Net,但C#端口應該不那么難。 事實上,這里(未經測試):

public interface ICustomEnum<T>
{
    ICustomEnum<T> FromT(T value);
    T Value { get; }

    // Implement using a sealed class with a private constructor 
    // that accepts and sets the Value property, 
    // one shared readonly property for each desired value in the enum,
    // and implicit conversions to and from T.
    // Then see this link to get intellisense support
    // that exactly matches a normal enum:
    // https://stackoverflow.com/questions/102084/hidden-features-of-vb-net/102217#102217
    // Note that the completion list only works for VB.
}

/// <completionlist cref="SessionKeys"/>
public sealed SessionKeys: ICustomEnum<string>
{
    private string _value;
    public string Value { get { return _value; } } 

    private SessionKeys(string value)
    {
        _value = value;
    }

    private static SessionKeys _value1 = new MyStringEnum("value1key");
    public static SessionKeys value1 { get { return _value1;} } 

    private static MyStringEnum _value2 = new MyStringEnum("value2key");
    public static MyStringEnum value2 { get { return _value2;} } 

    public static ICustomEnum<string> FromString(string value) 
    {
        // use reflection or a dictionary here if you have a lot of values
        switch( value )
        {
            case "value1key":
                return value1;
            case "value2key":
                return value2;
            default:
                return null; //or throw an exception
        }
    }

    public ICustomEnum<string> FromT(string value) 
    {
        return FromString(value);
    }

    public static implicit operator string(SessionKeys item)
    {
        return item.Value;
    }

    public static implicit operator SessionKeys(string value)
    {
        return FromString(value);
    }
}

真的不需要界面,但我保留它以提醒我如何實現它們。

問題是枚舉不僅僅是一個帶有一堆公共數值常量的靜態類。 枚舉是一種類型。 使用常量會丟失類型安全性。 如果使類的靜態成員與類的類型相同,則可以實現類型安全。

public sealed class SessionKey
{
    private _value; 
    private SessionKey( string value )
    {
        _value = value;
    }

    public string Value { get return _value; }

    public static readonly SessionKey Value1 = new SessionKey( "Value1" );
    public static readonly SessionKey Value2 = new SessionKey( "Value2" );
}

public class Something
{
    /* stuff */
    public void Foo( SessionKey sessionKey )
    {
        switch( sessionKey.Value )
        {
            case SessionKey.Value1.Value:
                DoBaz();
                break;
            case SessionKey.Value2.Value:
                DoBop();
                break;
            default:
                DoBar();
        }
    }   

    /* other stuff */
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM