Skip to content

Commit 47eeacd

Browse files
DoctorKrolicsiegfriedpammer
authored andcommitted
Add an option to not transform Activator.CreateInstance<T>() to new T()
1 parent 2bc26b4 commit 47eeacd

12 files changed

+794
-2
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\NoGenericTypeInstantiation.cs" />
156+
<None Include="TestCases\Ugly\NoGenericTypeInstantiation.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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace ICSharpCode.Decompiler.Tests.TestCases.Ugly
4+
{
5+
internal class NoGenericTypeInstantiation<TOnType> where TOnType : new()
6+
{
7+
public static TOnType CreateTOnType()
8+
{
9+
return Activator.CreateInstance<TOnType>();
10+
}
11+
12+
public static TOnMethod CreateTOnMethod<TOnMethod>() where TOnMethod : new()
13+
{
14+
return Activator.CreateInstance<TOnMethod>();
15+
}
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace ICSharpCode.Decompiler.Tests.TestCases.Ugly
2+
{
3+
internal class NoGenericTypeInstantiation<TOnType> where TOnType : new()
4+
{
5+
public static TOnType CreateTOnType()
6+
{
7+
return new TOnType();
8+
}
9+
10+
public static TOnMethod CreateTOnMethod<TOnMethod>() where TOnMethod : new()
11+
{
12+
return new TOnMethod();
13+
}
14+
}
15+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 tmppi3wbt
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 tmppi3wbt.tmp
23+
// MVID: {4130db71-37be-40af-9724-d988f7189866}
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.NoGenericTypeInstantiation`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 NoGenericTypeInstantiation`1::CreateTOnType
64+
65+
.method public hidebysig static !!TOnMethod
66+
CreateTOnMethod<.ctor TOnMethod>() cil managed
67+
{
68+
// Code size 39 (0x27)
69+
.maxstack 1
70+
.locals init (!!TOnMethod V_0,
71+
!!TOnMethod V_1)
72+
IL_0000: nop
73+
IL_0001: ldloca.s V_1
74+
IL_0003: initobj !!TOnMethod
75+
IL_0009: ldloc.1
76+
IL_000a: box !!TOnMethod
77+
IL_000f: brfalse.s IL_001c
78+
79+
IL_0011: ldloca.s V_1
80+
IL_0013: initobj !!TOnMethod
81+
IL_0019: ldloc.1
82+
IL_001a: br.s IL_0021
83+
84+
IL_001c: call !!0 [mscorlib]System.Activator::CreateInstance<!!0>()
85+
IL_0021: nop
86+
IL_0022: stloc.0
87+
IL_0023: br.s IL_0025
88+
89+
IL_0025: ldloc.0
90+
IL_0026: ret
91+
} // end of method NoGenericTypeInstantiation`1::CreateTOnMethod
92+
93+
.method public hidebysig specialname rtspecialname
94+
instance void .ctor() cil managed
95+
{
96+
// Code size 7 (0x7)
97+
.maxstack 8
98+
IL_0000: ldarg.0
99+
IL_0001: call instance void [mscorlib]System.Object::.ctor()
100+
IL_0006: ret
101+
} // end of method NoGenericTypeInstantiation`1::.ctor
102+
103+
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Ugly.NoGenericTypeInstantiation`1
104+
105+
106+
// =============================================================
107+
108+
// *********** DISASSEMBLY COMPLETE ***********************
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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 tmphku2gd
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+
18+
// --- The following custom attribute is added automatically, do not uncomment -------
19+
// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 07 01 00 00 00 00 )
20+
21+
.permissionset reqmin
22+
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}}
23+
.hash algorithm 0x00008004
24+
.ver 0:0:0:0
25+
}
26+
.module tmphku2gd.tmp
27+
// MVID: {1307db53-f6c4-45d3-a995-8cb116ea8821}
28+
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
29+
.custom instance void System.Runtime.CompilerServices.RefSafetyRulesAttribute::.ctor(int32) = ( 01 00 0B 00 00 00 00 00 )
30+
.imagebase 0x10000000
31+
.file alignment 0x00000200
32+
.stackreserve 0x00100000
33+
.subsystem 0x0003 // WINDOWS_CUI
34+
.corflags 0x00000001 // ILONLY
35+
36+
37+
// =============== CLASS MEMBERS DECLARATION ===================
38+
39+
.class private auto ansi sealed beforefieldinit Microsoft.CodeAnalysis.EmbeddedAttribute
40+
extends [mscorlib]System.Attribute
41+
{
42+
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
43+
.custom instance void Microsoft.CodeAnalysis.EmbeddedAttribute::.ctor() = ( 01 00 00 00 )
44+
.method public hidebysig specialname rtspecialname
45+
instance void .ctor() cil managed
46+
{
47+
// Code size 8 (0x8)
48+
.maxstack 8
49+
IL_0000: ldarg.0
50+
IL_0001: call instance void [mscorlib]System.Attribute::.ctor()
51+
IL_0006: nop
52+
IL_0007: ret
53+
} // end of method EmbeddedAttribute::.ctor
54+
55+
} // end of class Microsoft.CodeAnalysis.EmbeddedAttribute
56+
57+
.class private auto ansi sealed beforefieldinit System.Runtime.CompilerServices.RefSafetyRulesAttribute
58+
extends [mscorlib]System.Attribute
59+
{
60+
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
61+
.custom instance void Microsoft.CodeAnalysis.EmbeddedAttribute::.ctor() = ( 01 00 00 00 )
62+
.custom instance void [mscorlib]System.AttributeUsageAttribute::.ctor(valuetype [mscorlib]System.AttributeTargets) = ( 01 00 02 00 00 00 02 00 54 02 0D 41 6C 6C 6F 77 // ........T..Allow
63+
4D 75 6C 74 69 70 6C 65 00 54 02 09 49 6E 68 65 // Multiple.T..Inhe
64+
72 69 74 65 64 00 ) // rited.
65+
.field public initonly int32 Version
66+
.method public hidebysig specialname rtspecialname
67+
instance void .ctor(int32 A_1) cil managed
68+
{
69+
// Code size 15 (0xf)
70+
.maxstack 8
71+
IL_0000: ldarg.0
72+
IL_0001: call instance void [mscorlib]System.Attribute::.ctor()
73+
IL_0006: nop
74+
IL_0007: ldarg.0
75+
IL_0008: ldarg.1
76+
IL_0009: stfld int32 System.Runtime.CompilerServices.RefSafetyRulesAttribute::Version
77+
IL_000e: ret
78+
} // end of method RefSafetyRulesAttribute::.ctor
79+
80+
} // end of class System.Runtime.CompilerServices.RefSafetyRulesAttribute
81+
82+
.class private auto ansi sealed beforefieldinit System.Runtime.CompilerServices.CompilerFeatureRequiredAttribute
83+
extends [mscorlib]System.Attribute
84+
{
85+
.custom instance void [mscorlib]System.AttributeUsageAttribute::.ctor(valuetype [mscorlib]System.AttributeTargets) = ( 01 00 FF 7F 00 00 02 00 54 02 0D 41 6C 6C 6F 77 // ........T..Allow
86+
4D 75 6C 74 69 70 6C 65 01 54 02 09 49 6E 68 65 // Multiple.T..Inhe
87+
72 69 74 65 64 00 ) // rited.
88+
.method public hidebysig specialname rtspecialname
89+
instance void .ctor(string featureName) cil managed
90+
{
91+
// Code size 9 (0x9)
92+
.maxstack 8
93+
IL_0000: ldarg.0
94+
IL_0001: call instance void [mscorlib]System.Attribute::.ctor()
95+
IL_0006: nop
96+
IL_0007: nop
97+
IL_0008: ret
98+
} // end of method CompilerFeatureRequiredAttribute::.ctor
99+
100+
} // end of class System.Runtime.CompilerServices.CompilerFeatureRequiredAttribute
101+
102+
.class private auto ansi beforefieldinit System.Runtime.CompilerServices.IsExternalInit
103+
extends [mscorlib]System.Object
104+
{
105+
.method public hidebysig specialname rtspecialname
106+
instance void .ctor() cil managed
107+
{
108+
// Code size 8 (0x8)
109+
.maxstack 8
110+
IL_0000: ldarg.0
111+
IL_0001: call instance void [mscorlib]System.Object::.ctor()
112+
IL_0006: nop
113+
IL_0007: ret
114+
} // end of method IsExternalInit::.ctor
115+
116+
} // end of class System.Runtime.CompilerServices.IsExternalInit
117+
118+
.class private auto ansi sealed beforefieldinit System.Runtime.CompilerServices.RequiredMemberAttribute
119+
extends [mscorlib]System.Attribute
120+
{
121+
.custom instance void [mscorlib]System.AttributeUsageAttribute::.ctor(valuetype [mscorlib]System.AttributeTargets) = ( 01 00 8C 01 00 00 02 00 54 02 0D 41 6C 6C 6F 77 // ........T..Allow
122+
4D 75 6C 74 69 70 6C 65 00 54 02 09 49 6E 68 65 // Multiple.T..Inhe
123+
72 69 74 65 64 00 ) // rited.
124+
.method public hidebysig specialname rtspecialname
125+
instance void .ctor() cil managed
126+
{
127+
// Code size 8 (0x8)
128+
.maxstack 8
129+
IL_0000: ldarg.0
130+
IL_0001: call instance void [mscorlib]System.Attribute::.ctor()
131+
IL_0006: nop
132+
IL_0007: ret
133+
} // end of method RequiredMemberAttribute::.ctor
134+
135+
} // end of class System.Runtime.CompilerServices.RequiredMemberAttribute
136+
137+
.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Ugly.NoGenericTypeInstantiation`1<.ctor TOnType>
138+
extends [mscorlib]System.Object
139+
{
140+
.method public hidebysig static !TOnType
141+
CreateTOnType() cil managed
142+
{
143+
// Code size 11 (0xb)
144+
.maxstack 1
145+
.locals init (!TOnType V_0)
146+
IL_0000: nop
147+
IL_0001: call !!0 [mscorlib]System.Activator::CreateInstance<!TOnType>()
148+
IL_0006: stloc.0
149+
IL_0007: br.s IL_0009
150+
151+
IL_0009: ldloc.0
152+
IL_000a: ret
153+
} // end of method NoGenericTypeInstantiation`1::CreateTOnType
154+
155+
.method public hidebysig static !!TOnMethod
156+
CreateTOnMethod<.ctor TOnMethod>() cil managed
157+
{
158+
// Code size 11 (0xb)
159+
.maxstack 1
160+
.locals init (!!TOnMethod V_0)
161+
IL_0000: nop
162+
IL_0001: call !!0 [mscorlib]System.Activator::CreateInstance<!!0>()
163+
IL_0006: stloc.0
164+
IL_0007: br.s IL_0009
165+
166+
IL_0009: ldloc.0
167+
IL_000a: ret
168+
} // end of method NoGenericTypeInstantiation`1::CreateTOnMethod
169+
170+
.method public hidebysig specialname rtspecialname
171+
instance void .ctor() cil managed
172+
{
173+
// Code size 8 (0x8)
174+
.maxstack 8
175+
IL_0000: ldarg.0
176+
IL_0001: call instance void [mscorlib]System.Object::.ctor()
177+
IL_0006: nop
178+
IL_0007: ret
179+
} // end of method NoGenericTypeInstantiation`1::.ctor
180+
181+
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Ugly.NoGenericTypeInstantiation`1
182+
183+
184+
// =============================================================
185+
186+
// *********** DISASSEMBLY COMPLETE ***********************

0 commit comments

Comments
 (0)