2013-02-25 30 views
0
的GET SET列表<>

嗯,我想寫這整個「試」到下面開始設置方法嘗試的情況下爲兩個對象

try 
      { 
       string PUBLICKEY = ""; 
       switch (Path.GetFileName(file).Substring(0, 2)) 
       { 
        case "00": 
         PUBLICKEY = "453453453453"; 
         break; 
        case "01": 
         PUBLICKEY = "3453453453453"; 
         break; 
        case "02": 
         PUBLICKEY = "4434534563453"; 
         break; 
        case "03": 
         PUBLICKEY = "453453453453453"; 
         break; 
        case "04": 
         PUBLICKEY = "453453453453453"; 
         break; 
        case "06": 
         PUBLICKEY = "345345345345345"; 
         break; 
        case "09": 
         PUBLICKEY = "3456456456466"; 
         break; 
        case "11": 
         PUBLICKEY = "4564564564"; 
         break; 
        case "13": 
         PUBLICKEY = "456456456546564"; 
         break; 
        case "15": 
         PUBLICKEY = "464565464565456464456"; 
         break; 
        case "17": 
         PUBLICKEY = "456465564564546"; 
         break; 
        case "19": 
         PUBLICKEY = "213134378"; 
         break; 
        case "20": 
         PUBLICKEY = "456546456546564"; 
         break; 
        case "21": 
         PUBLICKEY = "786786786876"; 
         break; 
        case "26": 
         PUBLICKEY = "456456456456"; 
         break; 
        case "28": 
         PUBLICKEY = "456786786786"; 
         break; 
        case "34": 
         PUBLICKEY = "456546456"; 
         break; 
        case "35": 
         PUBLICKEY = "456546456546"; 
         break; 
        case "36": 
         PUBLICKEY = "456456464565465"; 
         break; 
        case "37": 
         PUBLICKEY = "45655466456"; 
         break; 
        default: 
         PUBLICKEY = "456456456456546"; //ZECHL 
         break; 
       } 

(真正的公共密鑰被拆除)

public class PublicKeys : List<PublicKey> 
    { 

    } 

    public class PublicKey 
    { 
     public string CC { get; set; } 
     public string publicKey { get; set; } 
    } 

我腦子頓時一片空白,我不能想了......

我想是這樣的:

public class PublicKeys : List<PublicKey> 
{ 
    public PublicKeys() 
    { 
     publicKeys(); 
    } 
    private void publicKeys() 
    { 
     this.Add(new PublicKey("00", "456456")); 
     this.Add(new PublicKey("01", "456456456")); 
     this.Add(new PublicKey("02", "45654645")); 
     this.Add(new PublicKey("03", "45645645646")); 
     this.Add(new PublicKey("04", "456456546456")); 
     this.Add(new PublicKey("06", "456456546456")); 
     this.Add(new PublicKey("09", "456456456456")); 
     this.Add(new PublicKey("11", "6456546456456")); 
     this.Add(new PublicKey("13", "456456456456")); 
     this.Add(new PublicKey("15", "45654645645")); 
     this.Add(new PublicKey("17", "456456546456564")); 
     this.Add(new PublicKey("19", "45645645645646565")); 
     this.Add(new PublicKey("20", "456456456546456")); 
     this.Add(new PublicKey("21", "4564565456645")); 
     this.Add(new PublicKey("26", "456465564456")); 
     this.Add(new PublicKey("28", "456456546456")); 
     this.Add(new PublicKey("34", "465456546654")); 
     this.Add(new PublicKey("35", "456456456456564")); 
     this.Add(new PublicKey("36", "45654456456465")); 
     this.Add(new PublicKey("37", "456456456456456")); 

     //PUBLICKEY = "4566554656654456564564456564"; //ZECHL 

    } 

    public string GetKey(string cc) 
    { 
     return getKey(cc); 
    } 

    private string getKey(string cc) 
    { 
     string key = ""; 
     this.ForEach(pk => 
     { 
      if (pk.CC == cc) 
      { 
       key = pk.publicKey; 
      } 
     } 
     ); 
     return key; 
    } 
} 

public class PublicKey 
{ 
    public string CC { get; set; } 
    public string publicKey { get; set; } 

    public PublicKey() 
    { 
    } 
    public PublicKey(string cc, string publicKey) 
    { 
     this.CC = cc; 
     this.publicKey = publicKey; 
    } 
} 

任何建議,將不勝感激..

TIA

+2

到底是什麼問題? – sloth 2013-02-25 10:56:00

+0

因爲CASE有一個DEFAULT,所以我不能工作,我需要.. – louispires 2013-02-25 10:57:01

+0

在你的'getKey'方法中,檢查傳遞的'cc'值是否存在,否則返回默認值。 – 2013-02-25 10:59:09

回答

0
private string getKey(string cc) 
{ 
    var key = this.Find(pk => pk.CC == cc); 
    if (key == null) 
     return defaultKey; 

    return key.publicKey; 
} 

順便說一句在C#,我們使用PascalCase爲方法和屬性的名稱。此外,我用字典存儲cc和公鑰對。你可以簡單地通過dictioary.ContainsKey(cc)檢查cc是否存在。

並考慮更好的名字。 PublicKeyPublicKey屬性令我感到困惑。什麼是CC?碳複製?


考慮一些映射類,它包含字典而不是從列表繼承。

public class PublicKeyMappings 
{ 
    Dictionary<string, string> _mappings = new Dictionary<string, string>(); 
    string _defaultKey; 

    public PublicKeyMappings AddMapping(string cc, string publicKey) 
    { 
     _mappings.Add(cc, publicKey); 
     return this; 
    } 

    public PublicKeyMappings SetDefaultKey(string publicKey) 
    { 
     _defaultKey = publicKey; 
     return this; 
    } 

    public string GetPublicKeyFor(string cc) 
    { 
     if (!_mappings.ContainsKey(cc)) 
      return _defaultKey; 

     return _mappings[cc]; 
    } 
} 

用法(流利API):

var mappings = new PublicKeyMappings() 
     .SetDefaultKey("4566554656654456564564456564") 
     .AddMapping("00", "456456") 
     .AddMapping("01", "456456456") 
     .AddMapping("02", "45654645"); 

var key = mappings.GetPublicKeyFor("02"); 
+1

甜美的男人!最後一個似乎是它爲我工作! – louispires 2013-02-25 12:12:43

相關問題