fork download
  1. using System;
  2. using System.Reflection;
  3.  
  4. class Base
  5. {
  6. public static Base Create(int args)
  7. {
  8. Console.WriteLine("Creating Base with args = {0}", args);
  9. return new Base();
  10. }
  11. }
  12.  
  13. class Utility
  14. {
  15. public static Base Create(string name, int args)
  16. {
  17. var type = typeof(Utility).Module.GetType(name);
  18. return Create(type, args);
  19. }
  20.  
  21. public static Base Create(Type type, int args)
  22. {
  23. var method = type.GetMethod("Create", BindingFlags.Public|BindingFlags.Static);
  24. return method.Invoke(null, new object[]{args}) as Base;
  25. }
  26. }
  27.  
  28. class Derived: Base
  29. {
  30. new public static Base Create(int args)
  31. {
  32. if (args <= 0)
  33. {
  34. return Utility.Create(typeof(Derived).BaseType, args);
  35. }
  36. else
  37. {
  38. Console.WriteLine("Creating Derived with args = {0}", args);
  39. return new Derived() as Base;
  40. }
  41. }
  42. }
  43.  
  44. public class Test
  45. {
  46. public static void Main()
  47. {
  48. Utility.Create("Base", 1234);
  49. Utility.Create("Derived", 0);
  50. Utility.Create("Derived", 1);
  51. }
  52. }
Success #stdin #stdout 0.03s 33976KB
stdin
Standard input is empty
stdout
Creating Base with args = 1234
Creating Base with args = 0
Creating Derived with args = 1