Skip to content

Commit 7d0451a

Browse files
Merge pull request #3497 from DoctorKrolic/no-new-T
Add an option to not transform `Activator.CreateInstance<T>()` to `new T()`
2 parents 2bc26b4 + 364c415 commit 7d0451a

17 files changed

+1180
-3
lines changed

ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@
152152
<Compile Include="TestCases\Pretty\Issue3442.cs" />
153153
<Compile Include="TestCases\Pretty\Issue3483.cs" />
154154
<Compile Include="TestCases\Pretty\PointerArithmetic.cs" />
155+
<Compile Include="TestCases\Ugly\NoNewOfT.cs" />
156+
<None Include="TestCases\Ugly\NoNewOfT.Expected.cs" />
155157
<Compile Include="TestCases\Pretty\ParamsCollections.cs" />
156158
<None Include="TestCases\VBPretty\VBAutomaticEvents.vb" />
157159
<Compile Include="TestCases\VBPretty\VBAutomaticEvents.cs" />
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace ICSharpCode.Decompiler.Tests.TestCases.Ugly
5+
{
6+
internal class NoNewOfT<TOnType> where TOnType : new()
7+
{
8+
public static TOnType CreateTOnType()
9+
{
10+
#if !ROSLYN
11+
#if OPT
12+
if (default(TOnType) != null)
13+
{
14+
return default(TOnType);
15+
}
16+
return Activator.CreateInstance<TOnType>();
17+
#else
18+
return (default(TOnType) == null) ? Activator.CreateInstance<TOnType>() : default(TOnType);
19+
#endif
20+
#else
21+
return Activator.CreateInstance<TOnType>();
22+
#endif
23+
}
24+
25+
public static T CreateUnconstrainedT<T>() where T : new()
26+
{
27+
#if !ROSLYN
28+
#if OPT
29+
if (default(T) != null)
30+
{
31+
return default(T);
32+
}
33+
return Activator.CreateInstance<T>();
34+
#else
35+
return (default(T) == null) ? Activator.CreateInstance<T>() : default(T);
36+
#endif
37+
#else
38+
return Activator.CreateInstance<T>();
39+
#endif
40+
}
41+
42+
public static T CreateClassT<T>() where T : class, new()
43+
{
44+
return Activator.CreateInstance<T>();
45+
}
46+
47+
public static T CollectionInitializer<T>() where T : IList<int>, new()
48+
{
49+
#if ROSLYN
50+
T result = Activator.CreateInstance<T>();
51+
result.Add(1);
52+
result.Add(2);
53+
result.Add(3);
54+
result.Add(4);
55+
result.Add(5);
56+
return result;
57+
#else
58+
T val = ((default(T) == null) ? Activator.CreateInstance<T>() : default(T));
59+
val.Add(1);
60+
val.Add(2);
61+
val.Add(3);
62+
val.Add(4);
63+
val.Add(5);
64+
return val;
65+
#endif
66+
}
67+
}
68+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections.Generic;
2+
3+
namespace ICSharpCode.Decompiler.Tests.TestCases.Ugly
4+
{
5+
internal class NoNewOfT<TOnType> where TOnType : new()
6+
{
7+
public static TOnType CreateTOnType()
8+
{
9+
return new TOnType();
10+
}
11+
12+
public static T CreateUnconstrainedT<T>() where T : new()
13+
{
14+
return new T();
15+
}
16+
17+
public static T CreateClassT<T>() where T : class, new()
18+
{
19+
return new T();
20+
}
21+
22+
public static T CollectionInitializer<T>() where T : IList<int>, new()
23+
{
24+
return new T() { 1, 2, 3, 4, 5 };
25+
}
26+
}
27+
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
2+
// .NET IL Disassembler. Version 9.0.4
3+
4+
5+
6+
// Metadata version: v4.0.30319
7+
.assembly extern mscorlib
8+
{
9+
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
10+
.ver 4:0:0:0
11+
}
12+
.assembly tmpvpdwr1
13+
{
14+
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
15+
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
16+
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
17+
.permissionset reqmin
18+
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}}
19+
.hash algorithm 0x00008004
20+
.ver 0:0:0:0
21+
}
22+
.module tmpvpdwr1.tmp
23+
// MVID: {8447adbb-757d-4626-b7b7-846d745d90d0}
24+
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
25+
.imagebase 0x10000000
26+
.file alignment 0x00000200
27+
.stackreserve 0x00100000
28+
.subsystem 0x0003 // WINDOWS_CUI
29+
.corflags 0x00000001 // ILONLY
30+
31+
32+
// =============== CLASS MEMBERS DECLARATION ===================
33+
34+
.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Ugly.NoNewOfT`1<.ctor TOnType>
35+
extends [mscorlib]System.Object
36+
{
37+
.method public hidebysig static !TOnType
38+
CreateTOnType() cil managed
39+
{
40+
// Code size 39 (0x27)
41+
.maxstack 1
42+
.locals init (!TOnType V_0,
43+
!TOnType V_1)
44+
IL_0000: nop
45+
IL_0001: ldloca.s V_1
46+
IL_0003: initobj !TOnType
47+
IL_0009: ldloc.1
48+
IL_000a: box !TOnType
49+
IL_000f: brfalse.s IL_001c
50+
51+
IL_0011: ldloca.s V_1
52+
IL_0013: initobj !TOnType
53+
IL_0019: ldloc.1
54+
IL_001a: br.s IL_0021
55+
56+
IL_001c: call !!0 [mscorlib]System.Activator::CreateInstance<!TOnType>()
57+
IL_0021: nop
58+
IL_0022: stloc.0
59+
IL_0023: br.s IL_0025
60+
61+
IL_0025: ldloc.0
62+
IL_0026: ret
63+
} // end of method NoNewOfT`1::CreateTOnType
64+
65+
.method public hidebysig static !!T CreateUnconstrainedT<.ctor T>() cil managed
66+
{
67+
// Code size 39 (0x27)
68+
.maxstack 1
69+
.locals init (!!T V_0,
70+
!!T V_1)
71+
IL_0000: nop
72+
IL_0001: ldloca.s V_1
73+
IL_0003: initobj !!T
74+
IL_0009: ldloc.1
75+
IL_000a: box !!T
76+
IL_000f: brfalse.s IL_001c
77+
78+
IL_0011: ldloca.s V_1
79+
IL_0013: initobj !!T
80+
IL_0019: ldloc.1
81+
IL_001a: br.s IL_0021
82+
83+
IL_001c: call !!0 [mscorlib]System.Activator::CreateInstance<!!0>()
84+
IL_0021: nop
85+
IL_0022: stloc.0
86+
IL_0023: br.s IL_0025
87+
88+
IL_0025: ldloc.0
89+
IL_0026: ret
90+
} // end of method NoNewOfT`1::CreateUnconstrainedT
91+
92+
.method public hidebysig static !!T CreateClassT<class .ctor T>() cil managed
93+
{
94+
// Code size 11 (0xb)
95+
.maxstack 1
96+
.locals init (!!T V_0)
97+
IL_0000: nop
98+
IL_0001: call !!0 [mscorlib]System.Activator::CreateInstance<!!0>()
99+
IL_0006: stloc.0
100+
IL_0007: br.s IL_0009
101+
102+
IL_0009: ldloc.0
103+
IL_000a: ret
104+
} // end of method NoNewOfT`1::CreateClassT
105+
106+
.method public hidebysig static !!T CollectionInitializer<.ctor (class [mscorlib]System.Collections.Generic.IList`1<int32>) T>() cil managed
107+
{
108+
// Code size 106 (0x6a)
109+
.maxstack 2
110+
.locals init (!!T V_0,
111+
!!T V_1,
112+
!!T V_2)
113+
IL_0000: nop
114+
IL_0001: ldloca.s V_2
115+
IL_0003: initobj !!T
116+
IL_0009: ldloc.2
117+
IL_000a: box !!T
118+
IL_000f: brfalse.s IL_001c
119+
120+
IL_0011: ldloca.s V_2
121+
IL_0013: initobj !!T
122+
IL_0019: ldloc.2
123+
IL_001a: br.s IL_0021
124+
125+
IL_001c: call !!0 [mscorlib]System.Activator::CreateInstance<!!0>()
126+
IL_0021: nop
127+
IL_0022: stloc.0
128+
IL_0023: ldloc.0
129+
IL_0024: box !!T
130+
IL_0029: ldc.i4.1
131+
IL_002a: callvirt instance void class [mscorlib]System.Collections.Generic.ICollection`1<int32>::Add(!0)
132+
IL_002f: nop
133+
IL_0030: ldloc.0
134+
IL_0031: box !!T
135+
IL_0036: ldc.i4.2
136+
IL_0037: callvirt instance void class [mscorlib]System.Collections.Generic.ICollection`1<int32>::Add(!0)
137+
IL_003c: nop
138+
IL_003d: ldloc.0
139+
IL_003e: box !!T
140+
IL_0043: ldc.i4.3
141+
IL_0044: callvirt instance void class [mscorlib]System.Collections.Generic.ICollection`1<int32>::Add(!0)
142+
IL_0049: nop
143+
IL_004a: ldloc.0
144+
IL_004b: box !!T
145+
IL_0050: ldc.i4.4
146+
IL_0051: callvirt instance void class [mscorlib]System.Collections.Generic.ICollection`1<int32>::Add(!0)
147+
IL_0056: nop
148+
IL_0057: ldloc.0
149+
IL_0058: box !!T
150+
IL_005d: ldc.i4.5
151+
IL_005e: callvirt instance void class [mscorlib]System.Collections.Generic.ICollection`1<int32>::Add(!0)
152+
IL_0063: nop
153+
IL_0064: ldloc.0
154+
IL_0065: stloc.1
155+
IL_0066: br.s IL_0068
156+
157+
IL_0068: ldloc.1
158+
IL_0069: ret
159+
} // end of method NoNewOfT`1::CollectionInitializer
160+
161+
.method public hidebysig specialname rtspecialname
162+
instance void .ctor() cil managed
163+
{
164+
// Code size 7 (0x7)
165+
.maxstack 8
166+
IL_0000: ldarg.0
167+
IL_0001: call instance void [mscorlib]System.Object::.ctor()
168+
IL_0006: ret
169+
} // end of method NoNewOfT`1::.ctor
170+
171+
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Ugly.NoNewOfT`1
172+
173+
174+
// =============================================================
175+
176+
// *********** DISASSEMBLY COMPLETE ***********************

0 commit comments

Comments
 (0)