I feel that your requirements are like expression trees. Is this what you want?
This is an expression to calculate the Pythagorean Theorem.
static void Main(string[] args)
{
LambdaExpression lambdaExpression = GetExpression();
var re= lambdaExpression.Compile().DynamicInvoke(3, 4);
Console.WriteLine(re);
Console.ReadLine();
}
public static LambdaExpression GetExpression()
{
var xParameter = Expression.Parameter(typeof(double), "x");
var yParameter = Expression.Parameter(typeof(double), "y");
var xSquared = Expression.Multiply(xParameter, xParameter);
var ySquared = Expression.Multiply(yParameter, yParameter);
var sum = Expression.Add(xSquared, ySquared);
var sqrtMethod = typeof(Math).GetMethod("Sqrt", new[] { typeof(double) });
var distance = Expression.Call(sqrtMethod, sum);
var distanceLambda = Expression.Lambda(
distance,
xParameter,
yParameter);
return distanceLambda;
}
Hope I did not misunderstand what you mean.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.