簡體   English   中英

如何創建字典<string, delegate>實例?</string,>

[英]How to create a Dictionary<string, delegate> instance?

遇到了這個問題,文檔沒有幫助。 我正在嘗試實現這個 class:

Class MyClass
{
    public delegate void MyDelegate();
    public Dictionary<string, MyDelegate> weaponEvents = new Dictionary<string, MyDelegate>();
    public List<string> weaponDefaultEvents = new List<string>() { "onAttack" };

    public void AddDefaultEvents()
    {
        MyDelegate aux;
        for (int i = 0; i < weaponDefaultEvents.Count; i++)
        {
            //weaponEvents.Add(weaponDefaultEvents[i], aux );-won't work since aux isn't assigned
            weaponEvents.Add(weaponDefaultEvents[i], #WHAT AM I MISSING HERE?# );
        }
    }
}

我想做的是這樣的:

myClassInstance.weaponEvents["onAttack"]?.Invoke()

@CoolBots 拯救了我的一天,這就是我想要的,謝謝!

  Class MyClass
    {
        public delegate void MyDelegate();
        public Dictionary<string, MyDelegate> weaponEvents = new Dictionary<string, MyDelegate>();
        public List<string> weaponDefaultEvents = new List<string>() { "onAttack" };

        public void AddDefaultEvents()
        {
            MyDelegate aux=default;
            for (int i = 0; i < weaponDefaultEvents.Count; i++)
            {
                                weaponEvents.Add(weaponDefaultEvents[i], aux );
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM