c# - Function dictionary without specifying parameters -


i have dictionary uses strings keys , action functions values.

is there way define dictionary without specifying parameters of each key's function? example, have function foo(int a, string b). possible assign dict['test'] = foo?

i apologize if question has been asked -- wasn't sure search for.

yes, use delegate instead of typed function. you'll call them dynamicinvoke() , parameters can passed array. in short:

dictionary<string, delegate>() _delegates;  void test1(int a) { } void test2(int a, int b) { }  void setup() {     _delegates = new dictionary<string, delegate>();     _delegates.add("test1", test1);     _delegates.add("test2", test2); }  void callit(string name, params object[] args) {     _delegates[name].dynamicinvoke(args); } 

try it:

callit("test1", 1); callit("test2", 1, 2); 

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -