SHARE
TWEET

ExpressionTrees_CreateRunTime

NelIfandieva Feb 15th, 2020 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. static void Main(string[] args)
  2.         {
  3.             var cat = new Cat();
  4.  
  5.             Expression<Func<Cat, string>> expr = c => c.CatMethod(42, "MyTested ASP.NET");
  6.  
  7.             var myClass = typeof(Cat);
  8.  
  9.             var numberConstant = Expression.Constant(42);
  10.             var textConstant = Expression.Constant("MyTested ASP.NET");
  11.  
  12.             var parameterExpression = Expression.Parameter(myClass, "c");
  13.  
  14.             var methodInfo = myClass.GetMethod(nameof(Cat.CatMethod));
  15.  
  16.             var methodCallExpression = Expression.Call(parameterExpression, methodInfo, numberConstant, textConstant);
  17.  
  18.             var lambdaExpression = Expression.Lambda<Func<Cat, string>>(methodCallExpression, parameterExpression);
  19.  
  20.             var function = lambdaExpression.Compile();
  21.  
  22.             Console.WriteLine(function(cat));
  23.         }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top