fork(2) download
  1. using System;
  2. using System.Reflection;
  3.  
  4. public class Test
  5. {
  6. delegate T Param<T>(params object[] args);
  7. static Param<T> skipArg<T>(Type type, string method, int ind, object value, Type[] types = null)
  8. {
  9. MethodInfo mi;
  10. if (types == null) mi = type.GetMethod(method);
  11. else mi = type.GetMethod(method, types);
  12. return delegate (object[] _args)
  13. {
  14. int len = _args.Length;
  15. object[] args = new object[len + 1];
  16. Array.Copy(_args, 0, args, 0, ind);
  17. Array.Copy(_args, ind, args, 1 + ind, len - ind);
  18. args[ind] = value;
  19. return (T)mi.Invoke(null, args);
  20. };
  21. }
  22.  
  23. static void Main()
  24. {
  25. Param<double>
  26. pow2_ = skipArg<double>(typeof(Math), "Pow", 0, 2),
  27. pow_2 = skipArg<double>(typeof(Math), "Pow", 1, 2);
  28. var hw = skipArg<int?>(typeof(Console), "WriteLine", 0, "Hellow world!", new Type[] { typeof(string) });
  29. hw(); hw(); hw();
  30. Console.WriteLine("2 ^ 3 = {0}", pow2_(3)); // 2 ^ 3 = 8
  31. Console.WriteLine("3 ^ 2 = {0}", pow_2(3)); // 3 ^ 2 = 9
  32. }
  33. }
Success #stdin #stdout 0s 131520KB
stdin
Standard input is empty
stdout
Hellow world!
Hellow world!
Hellow world!
2 ^ 3 = 8
3 ^ 2 = 9