c# - Lambda property value selector as parameter -


i have requirement modify method has parameter take lambda expression used on internal object return value of given property. forgive probable incorrect use of terminology first foray linq expressions!

i have tried searching answer, mentioned, terminology seems off , examples can find far complex or deal expressions collection functions such .where(), familiar with.

what have far (cut down version):

class myclass {     private myobject _myobject = new myobject() { name = "test", code = "t" };      private string mymethod(int testparameter, ??? selector)     {         //return _myobject.name;         //return _myobject.code;         return ???;     } } 

i call this:

string result = _myclassinstance.mymethod(1, (x => x.name)); 

or:

string result = _myclassinstance.mymethod(1, (x => x.code)); 

obviously parts missing selector parameter in mymethod, how apply local variable , how pass required property method when invoking it.

any appreciated, bonus points vb.net solutions unfortunately final implementation needs in our lone vb project!

private string mymethod(int testparameter, func<myobject, string> selector) {     return selector(_myobject); } 

when using func delegates, last parameter return type , first n-1 argument types. in case, there single myobject argument selector , returns string.

you can invoke like:

string name = _myclassinstance.mymethod(1, x => x.name); string result = _myclassinstance.mymethod(1, x => x.code); 

since return type of mymethod matches return type of selector delegate, make generic:

private t mymethod<t>(int testparameter, func<myobject, t> selector) {     myobject obj = //     return selector(obj); } 

edit: don't know vb.net looks be:

public function mymethod(testparameter integer, selector func(of myobject, string))     return selector(_myobject) end function 

and generic version be:

public function mymethod(of t)(testparameter integer, selector func(of myobject, t))     return selector(_myobject) end function 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -