in Education by
I want create expression with reflection in c#. I am target script is: using (var service = new MyProxy()) { var response = service.DoAction>>(srv => srv.GetProducts()); } How to (srv => srv.GetProducts() script is generate with expression tree? Edit: I am sorry for the wrong expression. wherein IProductsService, Response> and GetProducts are actually unknown types. I take generic on runtime. I wrote the following methods(CreateExpression) to try. But Expression property = Expression.Call(methodReturnType, methodName.Name, new Type[]{ } ,parameter); line gives the following error: No method 'GetProducts' exists on type 'ServiceFoundation.Response1[System.Collections.Generic.List1[ServiceFoundation.Products.Product]]'. Next in line for I have not tested it 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 I can express. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
If you have an Expression> you can call .Compile() on it and it returns an Action which you can then invoke normally. eg Expression> exp = resp => Console.WriteLine(resp); Action func = exp.Compile(); func(myResponse); However, if that's all you need to do, you might find it simpler not to use expressions at all; Action func = resp => Console.WriteLine(resp); func(myResponse);

Related questions

0 votes
    Is there a built in dll that will give me a list of links from a string. I want to send in ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    I have a Double which could have a value from around 0.000001 to 1,000,000,000.000 I wish to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    Is anyone using the Obout controls in C# .Net? How would you rate these controls, especially the Grid ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    What is the .vs folder used for exactly? It gets created at the base folder of my solution. I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Here is the example: if(value != ageValue) { ageValue = value; } I mean, if we assign the value ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I've created a seperate assembly with a class that is intended to be published through wmi. Then I' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I would like to know how to unload an assembly that is loaded into the main AppDomain. I have the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I would like to know how to unload an assembly that is loaded into the main AppDomain. I have the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    Having a problem getting a TreeView control to display node images. The code below works sometimes but fails ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm writing an application to start and monitor other applications in C#. I'm using the System.Diagnostics. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I am investigating the design of a work queue processor where the QueueProcessor retrieves a Command Pattern object ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
...