fork download
  1. using System;
  2. using System.Linq.Expressions;
  3.  
  4. public class Test
  5. {
  6. static T add<T>(T a, T b) {
  7. var p0 = Expression.Parameter(typeof(T),"a");
  8. var p1 = Expression.Parameter(typeof(T),"b");
  9. var ae = Expression.Add(p0, p1);
  10. var f = (Func<T,T,T>)Expression.Lambda(ae, p0, p1).Compile();
  11. return f(a, b);
  12. }
  13.  
  14. public static void Main()
  15. {
  16. int a = 4, b = 6;
  17. double x = 2.3, y = 5.2;
  18. Console.WriteLine("Sum of two ints = {0}", add(a, b));
  19. Console.WriteLine("Sum of two doubles = {0}", add(x, y));
  20. }
  21. }
Success #stdin #stdout 0.07s 34360KB
stdin
Standard input is empty
stdout
Sum of two ints = 10
Sum of two doubles = 7.5