Funcmean = (x, y) => ((x + y) / 2);
Console.WriteLine(mean(10, 20));
Use Expression class.
ParameterExpression x = Expression.Parameter(typeof(int), "x");
ParameterExpression y = Expression.Parameter(typeof(int), "y");
ConstantExpression two = Expression.Constant(2, typeof(int));
BinaryExpression add = Expression.Add(x, y);
BinaryExpression body = Expression.Divide(add, two);
Expression<Func<int, int, int>> meanExp =
Expression.Lambda<Func<int, int, int>>(
body,
new ParameterExpression[] { x, y });
Func<int, int, int> mean = meanExp.Compile();
Console.WriteLine(meanExp);
Console.WriteLine(mean(10, 20));
No comments:
Post a Comment