c# - Invoke random method from class -


if have this

ifoo f = foofactory.createfoo1(); 

how can pick random createfoo method invoke? foofactory static , contains around 15 different ifoo types. i'd randomly invoke 1 of them each time button pressed. tried this

var methods = typeof(foofactory).getmethods(bindingflags.public | bindingflags.static                                       | bindingflags.invokemethod);  int index = random.next(methods.length); ifoo randomfoo = (ifoo)(methods[index].invoke(null, null)); 

but results in system.reflection.targetparametercountexception. how can invoke random method?

the thing should change typeof(ifoo) since want invoke 1 of static methods of foofactory

try typeof(foofactory)

ps: assuming methods don't have parameters (or have same number , type of parameters)


Comments