How to call an appropriate method by string value from collection?

后端 未结 3 666
孤街浪徒
孤街浪徒 2021-01-28 13:06

I have a collection of strings. For example,

string[] coll={\"1\", \"2\", \"3\" ...\"100\"...\"150\"...} 

and I have respective methods for th

3条回答
  •  忘了有多久
    2021-01-28 13:47

    You can declare a dictionary with your keys and actions like

    Dictionary actions = new Dictionary()
    {
        { "1", MethodOne },
        { "2", ()=>Console.WriteLine("test") },
        ............
    };
    

    and invoke it as

    actions["1"]();
    

    PS: Presuming method void MethodOne(){ } is declared somewhere.

提交回复
热议问题