Call user defined functions from a For Loop in WPF C# -
i working on wpf mvvm project , having small requirement add function call in list , call them in loop.
suppose having function declaration :
public void getreports(string[] arguments) { // code } public void getusers(string[] arguments) { // code } public void getemployess(string[] arguments) { // code }
and call these functions using :
getreports(new string[] { "all" }); getusers(new string[] { "all" }); getemployess(new string[] { "all" });
now having requirement call these functions list or using other means can count of no. of functions process time calculation dynamically.
i can't use this:
list<string> listfns = new list<string>(); listfns.add("getreports(new string[] { "all" });"); listfns.add("getreports(new string[] { "all" });"); listfns.add("getreports(new string[] { "all" });");
and have iterated this:
(int = 0; <= listfns.count() - 1; i++) { listdatasets[i].tostring(); }
this may not sound have in way.
since methods have same signature can use list of delegates:
var actions = new list<action<string[]>>(); actions.add(this.getreports); actions.add(this.getusers); actions.add(this.getemployees);
and calling them easy:
foreach (var action in actions) { action(new[] { "all" }); }
if want parameters determined when method call scheduled (vs when performed) there number of options can use. example, packing parameters anonymous functions:
var actions = new list<action>(); actions.add(() => this.getreports(new[] { "foo" })); actions.add(() => this.getusers(new[] { "bar" })); // etc foreach (var action in actions) { action(); }
Comments
Post a Comment