String- Function dictionary c# where functions have different arguments

后端 未结 9 3289
温柔的废话
温柔的废话 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:29

    First of all, if you have to use a method dictionary, implement your methods to have the same signature ! (Or really similar whith less diffences possible). If not, you can still transform your method signature with lambda expression.

    Lambda expressions

        static string Method1(string a)
        {
            return a;
        }
    
        static void Method2(string a, string b)
        {
            Console.WriteLine(a + b);
        }
    
        static string Method3(string a, string b, int x)
        {
            return String.Format("a:{0} b:{1} x:{2}", a, b, x);
        }
    
        static int Method4(int x)
        {
            return x;
        }
    
        static void Main(string[] args)
        {
            var methods = new Dictionary>()
            {
                { "method1", (x, a, b, c) => Method1(a) },
                { "method2", (x, a, b, c) => { Method2(a, b); return ""; } },
                { "method3", (x, a, b, c) => Method3(a, b, x) },
                { "method4", (x, a, b, c) => Method4(x).ToString() },
            };
            foreach (var key in methods.Keys)
                Console.WriteLine(key + ": " + methods[key](1, "a", "b", "c"));
            Console.ReadKey();
        }
    

    As you can see, you will have to maintain the method signature for you dictionary to have all the possible arguments. IT'S UGLY ! But this will work, you won't have to care about which method is behind which string. They will all use the same parameters. But you will have to be very cautious when calling them since, you can easily make a mistake when you call them in the lambda expressions due to the big number of parameters that you will pass.


    Structure/Dictionary argument passing

        struct method_parameters
        {
            public string a;
            public string b;
            public int x;
        }
    
        static string Method1(method_parameters parameters)
        {
            return parameters.a;
        }
    
        static void Method2(method_parameters parameters)
        {
            Console.WriteLine(parameters.a + parameters.b);
        }
    
        static string Method3(method_parameters parameters)
        {
            return String.Format("a:{0} b:{1} x:{2}",
                parameters.a, parameters.b, parameters.x);
        }
    
        static int Method4(method_parameters parameters)
        {
            return parameters.x;
        }
    
        static void Main(string[] args)
        {
            method_parameters parameters = new method_parameters()
            {
                a = "a",
                b = "b",
                x = 1
            };
            var methods = new Dictionary>()
            {
                { "method1", Method1 },
                { "method2", (param) => { Method2(param); return ""; } },
                { "method3", Method3 },
                { "method4", (param) => Method4(param).ToString() },
            };
            foreach (var key in methods.Keys)
                Console.WriteLine(key + ": " + methods[key](parameters));
            Console.ReadKey();
        }
    

    This method is easier to maintain, since you will not have to update each method and entries of your dictionary if you have to add/change an argument. You will just have to modify your stucture/class/dictionary depending on what you chose as parameter storage. But to call a method you will have to update your structure first. And clear it if necessary !

提交回复
热议问题