.net - Call method Expression Tree in C# -


i want create expression reflection in c#.

i target script is:

using (var service = new myproxy<iproductsservice>()) {     var response = service.doaction<response<list<product>>>(srv => srv.getproducts()); } 

how (srv => srv.getproducts() script generate expression tree?

edit:

i sorry wrong expression. wherein iproductsservice, response<list<product>> , getproducts unknown types. take generic on runtime.

i wrote following methods(createexpression) try. expression property = expression.call(methodreturntype, methodname.name, new type[]{ } ,parameter); line gives following error:

no method 'getproducts' exists on type 'servicefoundation.response1[system.collections.generic.list1[servicefoundation.products.product]]'.

next in line have not tested yet.

method:

private void createexpression(type interfacetype, type methodreturntype, methodinfo methodname)     {     parameterexpression parameter = expression.parameter(interfacetype, "srv");     expression property = expression.call(methodreturntype, methodname.name, new type[]{ } ,parameter);            type expressiontype = typeof(expression<>);     type lambdatype = typeof(lambdaexpression);     type functype = typeof(func<,>);     type delegatetype = functype.makegenerictype(interfacetype, methodreturntype);     type expression = expressiontype.makegenerictype(delegatetype);      methodinfo mi = typeof(expression).getmethod("lambda");     methodinfo lambda = mi.makegenericmethod(delegatetype);     var ex = lambda.invoke(this, new object[] { delegatetype, property, parameter }); } 

hopefully can express.

if have

expression<action<response>> 

you can call

.compile() 

on , returns an

action<response> 

which can invoke normally.

eg

expression<action<response>> exp = resp => console.writeline(resp); action<response> func = exp.compile(); func(myresponse); 

however, if that's need do, might find simpler not use expressions @ all;

action<response> func = resp => console.writeline(resp); func(myresponse); 

Comments