用字典存储系列委托

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);

    }

}

 

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-28
  • 2021-11-26
  • 2022-03-07
  • 2022-12-23
  • 2022-12-23
  • 2021-07-05
猜你喜欢
  • 2022-12-23
  • 2022-01-10
  • 2021-11-04
  • 2021-12-22
  • 2021-05-29
  • 2021-12-22
相关资源
相似解决方案