简体   繁体   English

方法字典的C#匿名集合初始化程序

[英]C# Anonymous collection initializer for a method dictionary

Trying to make a dictionary that stores methods, those methods create stuff in windows forms, etc. 试图制作一个存储方法的字典,这些方法在Windows窗体中创建东西,等等。

The method takes no parameters and returns Dictionary of all the items created. 该方法不带任何参数,并返回创建的所有项目的Dictionary。

Dictionary<string, Func<Dictionary<string, dynamic>>> games = new 
Dictionary<string, Func<Dictionary<string, dynamic>>>()
{
    { "Buttoncatch", //I dont know what to write here
        {
           var objects = new Dictionary<string, dynamic>
           return objects;
        }
    }
};

I do not want to do this: 我不想这样做:

public Dictionary<string, dynamic> Buttoncatch()
{

}
games["Buttoncatch"] = Buttoncatch //dont even know what to write here, 
problems of self teaching

Why is the value type of your dictionary dynamic , it can be Func<Dictionary<string,dynamic>> . 为什么字典的值类型是dynamic ,可以是Func<Dictionary<string,dynamic>>

And the methods can be created as normal lambdas: 并且可以将这些方法创建为普通的lambda:

Dictionary<string, Func<Dictionary<string,dynamic>>> games = new     
Dictionary<string, Func<Dictionary<string, dynamic>>>
{
   {"Buttoncatch", 
    () => {
             var objects = new Dictionary<string, dynamic>
             return objects;
          }
   }
};

And when you declare the dictionary that way, the line 当您以这种方式声明字典时,

games["Buttoncatch"] = Buttoncatch;

will work, too. 也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM