Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RunClassConstructorUsedViaReflection.cs « Reflection « Mono.Linker.Tests.Cases « test - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18df634f51d9994eb92ddb98e766adf12acdee3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Reflection;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;
using System.Runtime.CompilerServices;

namespace Mono.Linker.Tests.Cases.Reflection
{
	[SetupCSharpCompilerToUse ("csc")]
	public class RunClassConstructorUsedViaReflection
	{
		public static void Main ()
		{
			TestRunClassConstructor ();
			TestNonKeptStaticConstructor ();
			TestNull ();
			TestDataFlowType ();
			TestIfElseUsingRuntimeTypeHandle (1);
			TestIfElseUsingType (1);
		}

		[Kept]
		[RecognizedReflectionAccessPattern]
		static void TestRunClassConstructor ()
		{
			RuntimeHelpers.RunClassConstructor (typeof (OnlyUsedViaReflection).TypeHandle);
		}

		[Kept]
		static void TestNonKeptStaticConstructor ()
		{
			var a = new NonKeptStaticConstructorClass ();
		}

		[Kept]
		[RecognizedReflectionAccessPattern]
		static void TestNull ()
		{
			Type type = null;
			RuntimeHelpers.RunClassConstructor (type.TypeHandle);
		}

		[Kept]
		static Type FindType ()
		{
			return null;
		}

		[Kept]
		[UnrecognizedReflectionAccessPattern (typeof (RuntimeHelpers), nameof (RuntimeHelpers.RunClassConstructor), new Type[] { typeof (RuntimeTypeHandle) },
			messageCode: "IL2059", message: "RunClassConstructor")]

		static void TestDataFlowType ()
		{
			Type type = FindType ();
			RuntimeHelpers.RunClassConstructor (type.TypeHandle);
		}

		[Kept]
		[RecognizedReflectionAccessPattern]
		[UnrecognizedReflectionAccessPattern (typeof (RuntimeHelpers), nameof (RuntimeHelpers.RunClassConstructor), new Type[] { typeof (RuntimeTypeHandle) },
			messageCode: "IL2059")]

		static void TestIfElseUsingRuntimeTypeHandle (int i)
		{
			RuntimeTypeHandle myType;
			if (i == 1) {
				myType = typeof (IfClass).TypeHandle;
			} else if (i == 2) {
				myType = FindType ().TypeHandle;
			} else {
				myType = typeof (ElseClass).TypeHandle;
			}
			RuntimeHelpers.RunClassConstructor (myType);
		}

		[Kept]
		[RecognizedReflectionAccessPattern]
		static void TestIfElseUsingType (int i)
		{
			Type myType;
			if (i == 1) {
				myType = typeof (IfClass2);
			} else if (i == 2) {
				myType = null;
			} else {
				myType = typeof (ElseClass2);
			}
			RuntimeHelpers.RunClassConstructor (myType.TypeHandle);
		}

		[Kept]
		[KeptMember (".cctor()")]
		class OnlyUsedViaReflection
		{
			[Kept]
			static int i = 5;
		}

		[Kept]
		[KeptMember (".ctor()")]
		class NonKeptStaticConstructorClass
		{
			static int i = 5;
		}

		[Kept]
		[KeptMember (".cctor()")]
		class IfClass
		{
			public IfClass ()
			{ }
			private IfClass (int foo)
			{ }
		}

		[Kept]
		[KeptMember (".cctor()")]
		class ElseClass
		{
			[Kept]
			static ElseClass ()
			{ }
			public ElseClass (int foo)
			{ }
		}
		[Kept]
		[KeptMember (".cctor()")]
		class IfClass2
		{
			public IfClass2 ()
			{ }
			private IfClass2 (int foo)
			{ }
		}

		[Kept]
		[KeptMember (".cctor()")]
		class ElseClass2
		{
			[Kept]
			static ElseClass2 ()
			{ }
			public ElseClass2 (int foo)
			{ }
		}
	}
}