I'd like to build Expression for something like this:
x => DoSomething(x)
Is it possible and how can I accomplish this?
I'd like to build Expression for something like this:
x => DoSomething(x)
Is it possible and how can I accomplish this?
You can do like this:
using System;
using System.Linq.Expressions;
public class Program
{
public static void Main()
{
Expression<Func<string, string>> func = (x) => DoSomething(x);
Console.WriteLine(func.ToString());
}
public static string DoSomething(string s)
{
return s; // just as sample
}
}
Here is working fiddle - https://dotnetfiddle.net/j1YKpM It will be parsed and Lambda will be saved as Expression
Do you mean Func<Tin,Tout>
delegate?
Basically you need a Func<Tin,Tout>
Func<Tin,Tout> func = x=> DoSomething(x)
where x
is of type Tin
and DoSomething
return Tout
type
Maybe the question was a bit unclear. This is what I meant:
var arg = Expression.Parameter(pluginType, "x");
var method = GetType().GetMethod("DoSomething");
var methodCall = Expression.Call(method, arg);
var lambda = Expression.Lambda(delegateType, methodCall, arg); // I was looking for this
Thats what I wanted. Thanks for your time :)