.net - Specify one parameter for Func at declaration time and another at invoke/execution time -
from research have not found evidence possible, wondering if there way or next cleanest solution?
i want avoid having pass argument generic function keep clean , improve modularity.
consider following generic looping function invokes predicate function check condition while looping collection:
private sub loopandapplycondition(of t)(byval collection idatacollection, byval condition func(of t, string, boolean), byref result list(of t)) if not collection.activeitems nothing , collection.count > 0 each record in collection '*** '*** pass in record predicate function here *** '*** dim meetscondition boolean = condition.invoke(ctype(record, t)) if meetscondition result.add(ctype(record, t)) next end if end sub
this defines predicate function(condition) , calls generic looping function, has attributename field pass predicate function.
public function alleditablerecords(of t)(byval collection idataobjectcollection, byval attributename string) list(of t) dim result new list(of t) '*** '*** pass in attributename field predicate function here *** '*** dim condition func(of t, string, boolean) = addressof checkifrecordiseditable loopandapplycondition(of t)(collection, condition, result) return result end function
this signature of predicate function:
private function checkifrecordiseditable(of t)(record t, attributename string) boolean 'returns conditionresult end function
so summarise, pass in string parameter checkifrecordiseditable via alleditablerecords function , generic record parameter via loopandapplycondition.
i don't think possible, please prove me wrong. i'm happy accept answer in c#, vb.net preferred.
no, not possible define parameters delegates while declaring them.
however, possible encapsulate func , parameters in own class:
public class recordcondition(of t) public property checkconditionhandler func(of t, string, boolean) public property attributename string end class
create recordcondition
in alleditablerecords:
public function alleditablerecords(of t)(byval collection idataobjectcollection, byval attributename string) list(of t) dim result new list(of t) dim recordcondition new recordcondition(of t) {.checkconditionhandler = addressof checkifrecordiseditable, .attributename=attributename} loopandapplycondition(of t)(collection, recordcondition, result) return result end function
call checkconditionhandler
in loopandapplycondition:
private sub loopandapplycondition(of t)(byval collection idatacollection, byval condition recordcondition(of t), byref result list(of t)) if not collection.activeitems nothing , collection.count > 0 each record in collection dim meetscondition boolean = condition.checkconditionhandler(record, condition.attributename) if meetscondition result.add(ctype(record, t)) next end if end sub
checkifrecordiseditable not change.
Comments
Post a Comment