fork download
  1. using System;
  2. using System.Reflection;
  3.  
  4. public class Test
  5. {
  6.  
  7.  
  8.  
  9. public static void Main()
  10. {
  11. AnimalEntityId id = AnimalEntityId.Parse(Guid.NewGuid().ToString());
  12. Console.WriteLine(id);
  13. }
  14. }
  15.  
  16. public class AnimalEntityId : EntityId<AnimalEntityId>
  17. {
  18. public AnimalEntityId()
  19. : base()
  20. {
  21. }
  22.  
  23. private AnimalEntityId(string value)
  24. : base(value)
  25. {
  26. }
  27.  
  28. }
  29.  
  30.  
  31. public abstract class EntityId<TEntityId>
  32. {
  33. private readonly System.Guid value;
  34.  
  35. public static TEntityId Parse(string val)
  36. {
  37. var constr = typeof(TEntityId).GetConstructor(
  38. BindingFlags.Instance | BindingFlags.NonPublic,
  39. null, new[]{typeof(string)}, null);
  40. if (constr == null) {
  41. throw new InvalidOperationException("No constructor");
  42. }
  43. return (TEntityId)constr.Invoke(new object[] {val});
  44. }
  45.  
  46. protected EntityId(string value)
  47. {
  48. this.value = System.Guid.Parse(value);
  49. }
  50.  
  51. protected EntityId()
  52. {
  53. this.value = System.Guid.NewGuid();
  54. }
  55. }
Success #stdin #stdout 0.1s 24328KB
stdin
Standard input is empty
stdout
AnimalEntityId