C# 尝试将案例放入获取集列表<&燃气轮机;两个物体的形状
我想把整个“TRY”写进下面的Get Set方法中C# 尝试将案例放入获取集列表<&燃气轮机;两个物体的形状,c#,list,get,set,try-catch,C#,List,Get,Set,Try Catch,我想把整个“TRY”写进下面的Get Set方法中 try { string PUBLICKEY = ""; switch (Path.GetFileName(file).Substring(0, 2)) { case "00": PUBLICKEY = "453453453453";
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;
}
(真正的公钥已被删除)
公共类公钥:列表
{
}
公共类公钥
{
公共字符串CC{get;set;}
公共字符串公钥{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;
}
}
公共类公钥:列表
{
公钥()
{
公钥();
}
私有无效公钥()
{
添加(新公钥(“00”、“456456”);
增加(新公钥(“01”、“45645656”);
增加(新公钥(“02”、“4565445”);
新增(新公钥(“03”、“45645646”);
新增(新公钥(“04”、“456456546456”);
本条增补(新公钥(“06”、“456456456”);
添加(新公钥(“09”、“456456456”);
新增(新公钥(“11”、“6456546456456”);
新增(新公钥(“13”、“456456456”);
新增(新公钥(“15”、“4565445645”);
新增(新公钥(“17”、“45645654564”);
新增(新公钥(“19”、“45645645646565”);
添加(新公钥(“20”、“456456546445656”);
本条增补(新公钥(“21”、“45645645”);
新增(新公钥(“26”、“45646556456”);
本条增补(新公钥(“28”、“456456456”);
新增(新公钥(“34”、“465456546654”);
新增(新公钥(“35”、“4564564564”);
新增(新公钥(“36”、“4565456456465”);
新增(新公钥(“37”、“45645644564456”);
//PUBLICKEY=“4566546566544564564456564”//ZECHL
}
公共字符串GetKey(字符串cc)
{
返回getKey(cc);
}
私有字符串getKey(字符串cc)
{
字符串键=”;
this.ForEach(pk=>
{
如果(pk.CC==CC)
{
key=pk.publicKey;
}
}
);
返回键;
}
}
公共类公钥
{
公共字符串CC{get;set;}
公共字符串公钥{get;set;}
公钥
{
}
公钥(字符串cc,字符串公钥)
{
this.CC=CC;
this.publicKey=publicKey;
}
}
如有任何建议,将不胜感激
短暂性脑缺血发作
顺便说一句,在C#中,我们使用PascalCase作为方法和属性名称。此外,我还使用字典存储cc
和公钥对。您只需通过dictionary.ContainsKey(cc)
检查cc是否存在
请考虑更好的名字。代码>公钥带有公钥
属性的类让我感到困惑。什么是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];
}
}
到底是什么问题?我所做的不起作用,因为案例有一个默认值,我需要..在
getKey
方法中,检查传递的cc
值是否存在,否则返回默认值。
private string getKey(string cc)
{
var key = this.Find(pk => pk.CC == cc);
if (key == null)
return defaultKey;
return key.publicKey;
}
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];
}
}
var mappings = new PublicKeyMappings()
.SetDefaultKey("4566554656654456564564456564")
.AddMapping("00", "456456")
.AddMapping("01", "456456456")
.AddMapping("02", "45654645");
var key = mappings.GetPublicKeyFor("02");