第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過C#字典的鍵的一部分獲取值

通過C#字典的鍵的一部分獲取值

C#
手掌心 2023-08-13 13:59:44
我有這本詞典。private Dictionary<string[], ICommand> commandsWithAttributes = new Dictionary<string[], ICommand>();我需要commandsWithAttributes通過部分鍵來查找元素。我的意思是說:"-?"- 是我用來查找物品的鑰匙。({"-t","--thread"},ICommand)({"-?","--help"},ICommand)<- 這就是我需要找到的。
查看完整描述

4 回答

?
SMILET

TA貢獻(xiàn)1796條經(jīng)驗 獲得超4個贊

請不要這樣做。字典針對一鍵到一值搜索進(jìn)行了優(yōu)化。


我對單個值使用多個鍵的建議如下:


private Dictionary<string, ICommand> commandsWithAttributes = new Dictionary<string, ICommand>();


var command1 = new Command(); //Whatever


commandsWithAttributes.Add("-t", command1);

commandsWithAttributes.Add("--thread", command1);


var command2 = new Command(); //Whatever


commandsWithAttributes.Add("-?", command2);

commandsWithAttributes.Add("--help", command2);


查看完整回答
反對 回復(fù) 2023-08-13
?
守著星空守著你

TA貢獻(xiàn)1799條經(jīng)驗 獲得超8個贊

這對{"-t","--thread"}稱為命令行選項。-t是選項的短名稱,--thread是其長名稱。當(dāng)您查詢字典以通過部分鍵獲取條目時,您實際上希望它由短名稱索引。我們假設(shè):

  • 所有選項都有短名稱

  • 所有選項都是字符串?dāng)?shù)組

  • 短名稱是字符串?dāng)?shù)組中的第一項

然后我們可以有這個比較器:

public class ShortNameOptionComparer : IEqualityComparer<string[]>

{

    public bool Equals(string[] x, string[] y)

    {

        return string.Equals(x[0], y[0], StringComparison.OrdinalIgnoreCase);

    }


    public int GetHashCode(string[] obj)

    {

        return obj[0].GetHashCode();

    }

}

...并將其插入字典中:


private Dictionary<string[], ICommand> commandsWithAttributes = new Dictionary<string[], ICommand>(new ShortNameOptionComparer());

要查找命令,我們必須使用string[]僅包含短名稱的命令,即-t: var value = dictionary[new [] { "-t" }]。或者將其包裝在擴(kuò)展方法中:


public static class CompositeKeyDictionaryExtensions

{

    public static T GetValueByPartialKey<T>(this IDictionary<string[], T> dictionary, string partialKey)

    {

        return dictionary[new[] { partialKey }];

    }

}

...并用它來獲取值:


var value = dictionary.GetValueByPartialKey("-t");


查看完整回答
反對 回復(fù) 2023-08-13
?
達(dá)令說

TA貢獻(xiàn)1821條經(jīng)驗 獲得超6個贊

您可以通過迭代所有鍵來搜索


var needle = "-?";

var kvp = commandsWithAttributes.Where(x => x.Key.Any(keyPart => keyPart == needle)).FirstOrDefault();

Console.WriteLine(kvp.Value);

但它不會給你使用字典帶來任何優(yōu)勢,因為你需要迭代所有的鍵。最好先扁平化你的層次結(jié)構(gòu)并搜索特定的鍵


var goodDict = commandsWithAttributes

    .SelectMany(kvp =>

        kvp.Key.Select(key => new { key, kvp.Value }))

    .ToDictionary(x => x.key, x => x.Value);


Console.WriteLine(goodDict["-?"]);


查看完整回答
反對 回復(fù) 2023-08-13
?
瀟瀟雨雨

TA貢獻(xiàn)1833條經(jīng)驗 獲得超4個贊

private Dictionary<string[], ICommand> commandsWithAttributes = new Dictionary<string[], ICommand>();


private ICommand FindByKey(string key)

    {

        foreach (var p in commandsWithAttributes)

        {

            if (p.Key.Any(k => k.Equals(key)))

            {

                return p.Value;

            }

        }


        return null;

    }

并調(diào)用像


ICommand ic = FindByKey("-?");


查看完整回答
反對 回復(fù) 2023-08-13
  • 4 回答
  • 0 關(guān)注
  • 249 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號