String- Function dictionary c# where functions have different arguments

后端 未结 9 2801
温柔的废话
温柔的废话 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条回答
  •  北海茫月
    2021-02-19 09:52

    I'd go with the ExpandoObject which was specifically meant to be able to have the CLR support dynamic languages (see IronPython, et al).

    static void Main()
    {
        dynamic expando = new ExpandoObject();
        expando.Do = new Func(MyFunc);
        expando.Do2 = new Func(MyFunc2);
    
        Console.WriteLine(expando.Do());
        Console.WriteLine(expando.Do2("args"));
    }
    
    static string MyFunc()
    {
        return "Do some awesome stuff";
    }
    
    static string MyFunc2(string arg)
    {
        return "Do some awesome stuff with " + arg;
    }
    

提交回复
热议问题