Guest User

Untitled

a guest
Jul 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. T t = new T()
  2.  
  3. public static T Create<T>() where T: new() {
  4. return new T();
  5. }
  6.  
  7. .method public hidebysig static !!T Create<.ctor T>() cil managed
  8. {
  9. .maxstack 2
  10. .locals init (
  11. [0] !!T local,
  12. [1] !!T local2)
  13. L_0000: ldloca.s local
  14. L_0002: initobj !!T
  15. L_0008: ldloc.0
  16. L_0009: box !!T
  17. L_000e: brfalse.s L_001a
  18. L_0010: ldloca.s local2
  19. L_0012: initobj !!T
  20. L_0018: ldloc.1
  21. L_0019: ret
  22. L_001a: call !!0 [mscorlib]System.Activator::CreateInstance<!!T>()
  23. L_001f: ret
  24. }
  25.  
  26. .method public hidebysig static !!T Create<.ctor T>() cil managed
  27. {
  28. .maxstack 2
  29. .locals init (
  30. [0] !!T CS$1$0000,
  31. [1] !!T CS$0$0001)
  32. L_0000: nop
  33. L_0001: ldloca.s CS$0$0001
  34. L_0003: initobj !!T
  35. L_0009: ldloc.1
  36. L_000a: box !!T
  37. L_000f: brfalse.s L_001c
  38. L_0011: ldloca.s CS$0$0001
  39. L_0013: initobj !!T
  40. L_0019: ldloc.1
  41. L_001a: br.s L_0021
  42. L_001c: call !!0 [mscorlib]System.Activator::CreateInstance<!!T>()
  43. L_0021: stloc.0
  44. L_0022: br.s L_0024
  45. L_0024: ldloc.0
  46. L_0025: ret
  47. }
  48.  
  49. public static void DoWork<T>() where T: new()
  50. {
  51. T t = new T();
  52. Console.WriteLine(t.ToString());
  53. }
  54.  
  55. .method public hidebysig
  56. instance void DoWork<.ctor T> () cil managed
  57. {
  58. // Method begins at RVA 0x2064
  59. // Code size 52 (0x34)
  60. .maxstack 2
  61. .locals init (
  62. [0] !!T t,
  63. [1] !!T CS$0$0000,
  64. [2] !!T CS$0$0001
  65. )
  66.  
  67. IL_0000: ldloca.s CS$0$0000
  68. IL_0002: initobj !!T
  69. IL_0008: ldloc.1
  70. IL_0009: box !!T
  71. IL_000e: brfalse.s IL_001b
  72.  
  73. IL_0010: ldloca.s CS$0$0001
  74. IL_0012: initobj !!T
  75. IL_0018: ldloc.2
  76. IL_0019: br.s IL_0020
  77.  
  78. IL_001b: call !!0 [mscorlib]System.Activator::CreateInstance<!!T>()
  79.  
  80. IL_0020: stloc.0
  81. IL_0021: ldloca.s t
  82. IL_0023: constrained. !!T
  83. IL_0029: callvirt instance string [mscorlib]System.Object::ToString()
  84. IL_002e: call void [mscorlib]System.Console::WriteLine(string)
  85. IL_0033: ret
  86. } // end of method Program::DoWork
  87.  
  88. public void DoWork<T>() where T : new()
  89. {
  90. T t = (default(T) == null) ? Activator.CreateInstance<T>() : default(T);
  91. Console.WriteLine(t.ToString());
  92. }
Add Comment
Please, Sign In to add comment