0

好吧,我想将整个“TRY”写入以下 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

4

1 回答 1

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和公钥对。您可以通过简单地检查 cc 是否存在dictioary.ContainsKey(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");
于 2013-02-25T10:59:35.033 回答