a list of dynamic functions and dynamically calling them

前端 未结 3 1133
长发绾君心
长发绾君心 2021-02-18 20:45

I would like to be able to store various static methods in a List and later look them up and dynamically call them.

Each of the static methods has different numbers of a

3条回答
  •  旧巷少年郎
    2021-02-18 20:55

    You can create a list of delegate-instances, using an appropriate delegate-type for each method.

    var list = new List
              {
                   new Func (X),
                   new Func (Y)
              };
    
    dynamic result = list[0](1, 2); // like X(1, 2)
    dynamic result2 = list[1](5, 10, "hello") // like Y(5, 10, "hello")
    

提交回复
热议问题