在数组中存储方法? [英] storing method in array?

查看:139
本文介绍了在数组中存储方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在数组中存储方法?你能给我举例吗?

解决方案

是的可能。答案 - 包含方法的数组 [ ^ ]。

Quote:

是的,有可能有这样的数组或列表。根据输入或输出参数的数量,您可以使用类似

列表< Func< T1,T2,TReturn>> 



Func< t1,>类型的实例是一种像

 TResult MyFunction(T1 input1,T2 input2)




使用委托,参考:根据数组中包含的字符串值调用/调用方法 [ ^ ]

了解有关<的更多信息a href =http://www.codeproject.com/Articles/602210/The-Power-of-Delegates-in-Csharp> C#代表的权力 [ ^ ]


< blockquote>

  var  methods =  new  System.Collections.Generic.Dictionary< string,System.Action>()
{
{ method1,()= > method1()},
{ method2,( )= > method2()}
};

方法[ method2]();



请参阅 [ ^ ]!



-KR


Is it possible to store a method in an array? and can you give me examples?

解决方案

Yes possible. Answers at - Array containing Methods[^].

Quote:

Yes, it is possible to have such an array or list. Depending on the number of input or output parameters, you'd use something like

List<Func<T1, T2, TReturn>>


An instance of type Func<t1,> is a method like

TResult MyFunction(T1 input1, T2 input2)



Use delegate, refer: Call/Invoke a method based on a string value contained in an array[^]
Learn more about The Power of Delegates in C#[^]


var methods = new System.Collections.Generic.Dictionary<string, System.Action>()
      {
          {"method1", () => method1() },
          {"method2", () => method2() }
      };

methods["method2"]();


See this[^] !

-KR


这篇关于在数组中存储方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆