我想将整个“TRY”写入以下Get Set Method
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
答案 0 :(得分:0)
private string getKey(string cc)
{
var key = this.Find(pk => pk.CC == cc);
if (key == null)
return defaultKey;
return key.publicKey;
}
在C#中BTW我们使用PascalCase作为方法和属性名称。此外,我使用字典存储cc
和公钥对。您只需通过dictioary.ContainsKey(cc)
检查cc是否存在。
考虑更好的名字。 PublicKey
具有PublicKey
属性的类对我来说很困惑。什么是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];
}
}
用法(Fluent API):
var mappings = new PublicKeyMappings()
.SetDefaultKey("4566554656654456564564456564")
.AddMapping("00", "456456")
.AddMapping("01", "456456456")
.AddMapping("02", "45654645");
var key = mappings.GetPublicKeyFor("02");