python - Generic class for functions with any signature c# -
i trying convert python code c#
class fwrapper: def __init_ _(self,function,childcount,name): self.function=function self.childcount=childcount self.name=name class node: def __init_ _(self,fw,children): self.function=fw.function self.name=fw.name self.children=children def evaluate(self,inp): results=[n.evaluate(inp) n in self.children] return self.function(results)
i finding difficult achieve in c# . 1. in class fwrapper.function can take function signature . 2. obtain return type of function can used mention return type of evaluate function
thanks lot
not entirely sure trying do, if had this:
public class fwrapper<tchild, tresult>{ private int childcount; private string name; private func<tchild, tresult> function; public func<tchild, tresult> function { { return function; } } public fwrapper(func<tchild, tresult> function, int childcount, string name){ this.childcount = childcount; this.name = name; this.function = function; } } public class node<tchild, tresult>{ private fwrapper<tchild, tresult> fw; private ienumerable<tchild> children; public node(fwrapper<tchild, tresult> fw, ienumerable<tchild> children){ this.fw = fw; this.children = children; } public ienumerable<tresult> evaluate(){ var results = new list<tresult>(); foreach(var c in children){ results.add(fw.function(c)); } return results; } }
you have fwrapper class takes in function takes tchild parameter , returns tresult , node class works same tchild , tresult types, so, example (trivial example)
//generic function takes int , returns string var func = new func<int, string>(childint => { var str = "'" + childint.tostring() + "'"; return str; }); var fw = new fwrapper<int, string>(func, 10, "foobar"); var children = new list<int>(){ 1,2,3,4,5,6,7,8,9,10 }; var node = new node<int, string>(fw, children); var results = node.evaluate(); foreach(var r in results){ console.writeline(r); } //'1' //'2' //.. //.. //'9' //'10'
Comments
Post a Comment