用字典存储系列委托

用字典存储系列委托

http://stackoverflow.com/questions/3813261/how-to-store-delegates-in-a-list

1.System.Collections.Generic.Dictionary<string, System.Delegate>  ;

2.dic.Add("action", new Action(() => Console.WriteLine("action called!")));

3. Dictionary<string, Func<int, int>> fnDict = new Dictionary<string, Func<int, int>>();

        Func<int, int> fn = (a) => a + 1;

        fnDict.Add("1", fn);

        var re = fnDict["1"](5);

 

4.class Program

{

    public delegate double MethodDelegate( double a );

 

    static void Main()

    {

        var delList = new List<MethodDelegate> {Foo, FooBar};

 

 

        Console.WriteLine(delList[0](12.34));

        Console.WriteLine(delList[1](16.34));

 

        Console.ReadLine();

    }

 

    private static double Foo(double a)

    {

        return Math.Round(a);

    }

 

    private static double FooBar(double a)

    {

        return Math.Round(a);

    }

}

原文地址:https://www.cnblogs.com/baiyu/p/3365253.html