String- Function dictionary c# where functions have different arguments

后端 未结 9 2885
温柔的废话
温柔的废话 2021-02-19 09:13

Basically I\'m trying to make a string to function dictionary in c#, I\'ve seen it done like this:

Dictionary>
<         


        
9条回答
  •  Happy的楠姐
    2021-02-19 09:35

    (edited) One easy but nasty solution could be something like this,

    private void methodDictionary()
    {
        var infos = new Dictionary();
        infos.Add("a", this.GetType().GetMethod("a"));
        infos.Add("b", this.GetType().GetMethod("b"));
    
        MethodInfo a = infos["a"];
        a.Invoke(this, new[] { "a1", "b1" });
    
        MethodInfo b = infos["b"];
        b.Invoke(this, new object[] { 10, "b1", 2.056 });
    }
    
    public void a(string a, string b)
    {
        Console.WriteLine(a);
        Console.WriteLine(b);
    }
    
    public void b(int a, string b, double c)
    {
        Console.WriteLine(a);
        Console.WriteLine(b);
        Console.WriteLine(c);
    }
    

提交回复
热议问题