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

RuntimeReflectionExtensionsCalls.cs « Reflection « Mono.Linker.Tests.Cases « test - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 068975d4c48ea28944e2d07a47ecaec29caa5c9d (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Mono.Linker.Tests.Cases.Expectations.Assertions;

namespace Mono.Linker.Tests.Cases.Reflection
{
#pragma warning disable 67 // The event {event} is not used
	public class RuntimeReflectionExtensionsCalls
	{
		public static void Main ()
		{
			TestGetRuntimeEvent ();
			TestGetRuntimeField ();
			TestGetRuntimeMethod ();
			TestGetRuntimeProperty ();

		}

		#region GetRuntimeEvent
		[Kept]
		[UnrecognizedReflectionAccessPattern (typeof (RuntimeReflectionExtensions), nameof (RuntimeReflectionExtensions.GetRuntimeEvent),
			new Type[] { typeof (Type), typeof (string) }, messageCode: "IL2072")]
		public static void TestGetRuntimeEvent ()
		{
			typeof (ClassWithKeptMembers).GetRuntimeEvent ("PublicEvent");
			typeof (ClassWithUnkeptMembers).GetRuntimeEvent ("PrivateEvent");
			typeof (ClassWithUnkeptMembers).GetRuntimeEvent ("ProtectedEvent");
			GetClassWithEvent ().GetRuntimeEvent ("This string will not be reached");
			typeof (Derived).GetRuntimeEvent ("Event");
			GetUnknownType ().GetRuntimeEvent (GetUnknownString ()); // UnrecognizedReflectionAccessPattern
		}
		#endregion

		#region GetRuntimeField
		[Kept]
		[UnrecognizedReflectionAccessPattern (typeof (RuntimeReflectionExtensions), nameof (RuntimeReflectionExtensions.GetRuntimeField),
			new Type[] { typeof (Type), typeof (string) }, messageCode: "IL2072")]
		public static void TestGetRuntimeField ()
		{
			typeof (ClassWithKeptMembers).GetRuntimeField ("PublicField");
			typeof (ClassWithUnkeptMembers).GetRuntimeField ("PrivateField");
			typeof (ClassWithUnkeptMembers).GetRuntimeField ("ProtectedField");
			GetClassWithField ().GetRuntimeField ("This string will not be reached");
			typeof (Derived).GetRuntimeField ("Field");
			GetUnknownType ().GetRuntimeField (GetUnknownString ()); // UnrecognizedReflectionAccessPattern
		}
		#endregion

		#region GetRuntimeMethod
		[Kept]
		[UnrecognizedReflectionAccessPattern (typeof (RuntimeReflectionExtensions), nameof (RuntimeReflectionExtensions.GetRuntimeMethod),
			new Type[] { typeof (Type), typeof (string), typeof (Type[]) }, messageCode: "IL2072")]
		public static void TestGetRuntimeMethod ()
		{
			typeof (ClassWithKeptMembers).GetRuntimeMethod ("PublicMethod", Type.EmptyTypes);
			typeof (ClassWithUnkeptMembers).GetRuntimeMethod ("PrivateMethod", Type.EmptyTypes);
			typeof (ClassWithUnkeptMembers).GetRuntimeMethod ("ProtectedMethod", Type.EmptyTypes);
			GetClassWithMethod ().GetRuntimeMethod ("This string will not be reached", Type.EmptyTypes);
			typeof (Derived).GetRuntimeMethod ("Method", Type.EmptyTypes);
			GetUnknownType ().GetRuntimeMethod (GetUnknownString (), Type.EmptyTypes); // UnrecognizedReflectionAccessPattern
		}
		#endregion

		#region GetRuntimeProperty
		[Kept]
		[UnrecognizedReflectionAccessPattern (typeof (RuntimeReflectionExtensions), nameof (RuntimeReflectionExtensions.GetRuntimeProperty),
			new Type[] { typeof (Type), typeof (string) }, messageCode: "IL2072")]
		public static void TestGetRuntimeProperty ()
		{
			typeof (ClassWithKeptMembers).GetRuntimeProperty ("PublicProperty");
			typeof (ClassWithUnkeptMembers).GetRuntimeProperty ("PrivateProperty");
			typeof (ClassWithUnkeptMembers).GetRuntimeProperty ("ProtectedProperty");
			GetClassWithProperty ().GetRuntimeProperty ("This string will not be reached");
			typeof (Derived).GetRuntimeProperty ("Property");
			GetUnknownType ().GetRuntimeProperty (GetUnknownString ()); // UnrecognizedReflectionAccessPattern
		}
		#endregion

		#region Helpers
		class ClassWithKeptMembers
		{
			[Kept]
			[KeptEventAddMethod]
			[KeptEventRemoveMethod]
			[method: ExpectBodyModified]
			public event EventHandler<EventArgs> PublicEvent;

			[Kept]
			public int PublicField;

			[Kept]
			public void PublicMethod (int arg)
			{
			}

			[Kept]
			public long PublicProperty { [Kept][ExpectBodyModified] get; [Kept][ExpectBodyModified] set; }
		}

		[Kept]
		class ClassWithUnkeptMembers
		{
			private event EventHandler<EventArgs> PrivateEvent;

			private int PrivateField;

			private void PrivateMethod (int arg)
			{
			}

			private long PrivateProperty { get; set; }

			protected event EventHandler<EventArgs> ProtectedEvent;

			protected int ProtectedField;

			protected void ProtectedMethod (int arg)
			{
			}

			protected long ProtectedProperty { get; set; }
		}

		class ClassWithEvent
		{
			[Kept]
			[KeptBackingField]
			[KeptEventAddMethod]
			[KeptEventRemoveMethod]
			public static event EventHandler<EventArgs> Event;
		}

		class ClassWithField
		{
			[Kept]
			public static int Field;
		}

		class ClassWithMethod
		{
			[Kept]
			public static void Method (int arg)
			{
			}
		}

		class ClassWithProperty
		{
			[Kept]
			[KeptBackingField]
			public static long Property { [Kept] get; [Kept] set; }
		}

		[Kept]
		class Base
		{
			[Kept]
			[KeptEventAddMethod]
			[KeptEventRemoveMethod]
			[method: ExpectBodyModified]
			public event EventHandler<EventArgs> Event;

			[Kept]
			public int Field;

			[Kept]
			public void Method (int arg)
			{
			}

			[Kept]
			public long Property { [Kept][ExpectBodyModified] get; [Kept][ExpectBodyModified] set; }
		}

		[Kept]
		[KeptBaseType (typeof (Base))]
		class Derived : Base
		{
		}

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

		[Kept]
		private static string GetUnknownString ()
		{
			return null;
		}

		[Kept]
		[return: KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))]
		[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicEvents)]
		private static Type GetClassWithEvent ()
		{
			return typeof (ClassWithEvent);
		}

		[Kept]
		[return: KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))]
		[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)]
		private static Type GetClassWithField ()
		{
			return typeof (ClassWithField);
		}

		[Kept]
		[return: KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))]
		[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
		private static Type GetClassWithMethod ()
		{
			return typeof (ClassWithMethod);
		}

		[Kept]
		[return: KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))]
		[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicProperties)]
		private static Type GetClassWithProperty ()
		{
			return typeof (ClassWithProperty);
		}
		#endregion
	}
}