fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static Func<T, TResult> MakeFunctionWithCounter<T, TResult>(Func<T, TResult> func)
  6. {
  7. int counter = 0;
  8. return t => { counter++; return func(t); };
  9. }
  10.  
  11. public static int Counts<T, TResult>(Func<T, TResult> call)
  12. {
  13. return (int)call.Target.GetType().GetField("counter").GetValue(call.Target);
  14. }
  15.  
  16. static void Main(string[] args)
  17. {
  18. var f = new Func<double, double>(d => d*d);
  19. f = MakeFunctionWithCounter(f);
  20. f(1);
  21. f(2);
  22. f(3);
  23. Console.WriteLine(Counts(f));
  24. }
  25. }
Runtime error #stdin #stdout 0.03s 37616KB
stdin
Standard input is empty
stdout
Standard output is empty