簡體   English   中英

如何添加將類引用數據返回到我的C#類中的函數?

[英]How can I add a function that returns class reference data into my c# class?

我有以下課程:

public class Content {
    public int Key { get; set; }
    public int Order { get; set; }
    public string Title { get; set; }
}

我具有以下函數,該函數根據id返回內容類型代碼。

protected string getType(string id) {
    switch(id.Substring(2, 2)) {
        case "00": return ("14");
        case "1F": return ("11");
        case "04": return ("10");
        case "05": return ("09");
        default: return ("99");
    }
}

盡管id並非內容類的一部分,但類和函數始終一起使用。

有什么辦法可以讓我將此函數完全適合我的班級嗎? 我當時在想一個枚舉或某個固定的東西,但是我對C#的了解還不足以讓我知道如何做到這一點。 我希望有人能給我和例子。

更新:

我喜歡以下建議:

public static readonly Dictionary<String, String> IdToType = 
    new Dictionary<string, string>
    {
        {"00", "14"},
        {"1F", "11"},
        {"04", "10"},
        {"05", "09"},
        //etc.
    };

但我不知道該如何適應我的班級。 外面有誰可以向我展示嗎? 我想做的就是寫這樣的東西:

Content.getType("00")

根據建議將數據存儲在字典中。

這可能不是您要查找的內容,但是我會使用字符串將字典字符串化。

如果要使其成為類的公共靜態成員,那可能就是您想要的。

例如:

public static readonly Dictionary<String, String> IdToType = 
    new Dictionary<string, string>
    {
        {"00", "14"},
        {"1F", "11"},
        {"04", "10"},
        {"05", "09"},
        //etc.
    };

您正在尋找類似這樣的東西嗎?

public class Content
{
    public int Key { get; set; }
    public int Order { get; set; }
    public string Title { get; set; }

    public static string getType(string id)
    {
        switch (id.Substring(2, 2))
        {
            case "00": return ("14");
            case "1F": return ("11");
            case "04": return ("10");
            case "05": return ("09");
            default: return ("99");
        }
    }
}

該方法可以這樣調用: Content.getType("00")


另外:按照慣例,C#中的方法名稱應使用Pascal大小寫,因此您的方法名稱應為GetType 您可能已經發現, System.Object上已經有一個名為GetType的方法,因此您可能想提出一個更具描述性的名稱。

看來您需要將枚舉與擴展方法結合起來...

例如

static string Default = "99";
public static readonly Dictionary<string, string> Cache = new Dictionary<string,string>(){
    {"00", "14"},
    {"1F", "11"},
    {"04", "10"},
    {"05", "09"},
    //etc
}
public static getType(Content this){
    if(Cache.ContainsKey(this.typeId)) return Cache[this.typeId];
    else return Default;
}
//Add other types as needed

或參閱此帖子以獲取TypeSafe枚舉模式的示例: C#字符串枚舉

芽,

只是為了解釋@DLH的答案:

public class Content 
{
 public int Key { get; set; }
 public int Order { get; set; }
 public string Title { get; set; }

 public static readonly Dictionary<String, String> getType = 
       new Dictionary<string, string>
 {
     {"00", "14"},
     {"1F", "11"},
     {"04", "10"},
    {"05", "09"},
    //etc.
 };
}

然后將允許您執行以下操作:

string value = Content.getType["00"];

您可以定義一個與字符串可比的類...

然后,您可以使用所需的內容值定義一個枚舉。

然后,您可以添加運算符重載,以處理字符串,int或long等。

然后,您將為枚舉類型添加運算符重載。

使用這種方法,您將不需要字典,甚至不需要枚舉,因為您將在Content類中聲明const readonly屬性,例如public static readonly Type0 = "00";

盡管這很相似,但實際上比使用類型安全枚舉模式要少,並且它使您能夠聲明實常數。

暫無
暫無

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

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