forked from Ourpalm/ILRuntime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReflectionTest.cs
442 lines (356 loc) · 13.1 KB
/
ReflectionTest.cs
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using ILRuntimeTest.TestFramework;
namespace TestCases
{
public class ReflectionTest
{
public static void ReflectionTest01()
{
Console.Write("Test typeof...");
Console.WriteLine(typeof(TestCls) == typeof(TestCls));
Console.WriteLine(typeof(TestCls) == typeof(TestCls2));
Console.WriteLine(typeof(TestCls) != typeof(TestCls2));
}
public static void ReflectionTest02()
{
var type = typeof(TestCls);
var method = type.GetMethod("foo");
var method2 = type.GetMethod("bar");
method.Invoke(new TestCls(), new object[] { 100 });
method2.Invoke(null, null);
}
public static void ReflectionTest03()
{
var ins = new TestCls();
var type = ins.GetType();
var method = type.GetMethod("foo");
var method2 = type.GetMethod("bar");
method.Invoke(ins, new object[] { 100 });
method2.Invoke(null, null);
}
public static void ReflectionTest04()
{
var t = Type.GetType("TestCases.ReflectionTest/TestCls2");
var obj = Activator.CreateInstance(t);
Console.WriteLine(obj);
var arr = t.GetCustomAttributes(typeof(TestAttribute), false);
foreach (var i in arr)
{
TestAttribute a = (TestAttribute)i;
Console.WriteLine(a.TestProp);
}
arr = typeof(TestCls).GetCustomAttributes(false);
foreach (var i in arr)
{
Console.WriteLine(i);
}
}
public static void ReflectionTest05()
{
var fi = typeof(TestCls).GetField("aa");
var fi2 = typeof(TestCls).GetField("bb");
var a = new TestCls();
Console.WriteLine("aa=" + fi.GetValue(a));
Console.WriteLine("bb=" + fi2.GetValue(null));
fi.SetValue(a, 123);
fi2.SetValue(null, 233);
Console.WriteLine("aa=" + fi.GetValue(a));
Console.WriteLine("bb=" + fi2.GetValue(null));
a.foo(110);
var arr = fi.GetCustomAttributes(false);
foreach (var i in arr)
{
Console.WriteLine(i);
}
}
public static void ReflectionTest06()
{
var arr = typeof(SingletonTest).GetFields();
foreach (var i in arr)
{
Console.WriteLine(i);
}
var fi = typeof(SingletonTest).GetField("testFloat");
fi.SetValue(SingletonTest.Inst, 1.2f);
Console.WriteLine(SingletonTest.Inst.testFloat);
}
public static void ReflectionTest07()
{
//-----------------------------CLR-----------------------------//
var isDefined = typeof(TestCls2).IsDefined(typeof(ObsoleteAttribute), true);
if (isDefined == false)
{
throw new Exception("isDefeinded == false 1");
}
isDefined = typeof(TestCls2).GetProperty("Attribute_prop").IsDefined(typeof(TestCLRAttribute), true);
if (isDefined == false)
{
throw new Exception("isDefeinded == false 2");
}
isDefined = typeof(TestCls2).GetField("Attribute_field").IsDefined(typeof(TestCLRAttribute), true);
if (isDefined == false)
{
throw new Exception("isDefeinded == false 3");
}
}
public static void ReflectionTest08()
{
//-----------------------------CLR-----------------------------//
var isDefined = typeof(TestCls2).IsDefined(typeof(TestAttribute), true);
if (isDefined == false)
{
throw new Exception("isDefeinded == false 1");
}
isDefined = typeof(TestCls2).GetProperty("ILAttribute_prop").IsDefined(typeof(TestAttribute), true);
if (isDefined == false)
{
throw new Exception("isDefeinded == false 2");
}
isDefined = typeof(TestCls2).GetField("ILAttribute_field").IsDefined(typeof(TestAttribute), true);
if (isDefined == false)
{
throw new Exception("isDefeinded == false 3");
}
}
[Obsolete("gasdgas")]
class TestCls
{
[ContextStatic]
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Collapsed)]
int aa = 203;
static int bb = 333;
[Microsoft.SqlServer.Server.SqlFunction(DataAccess = Microsoft.SqlServer.Server.DataAccessKind.Read)]
public TestCls foo(int b)
{
Console.WriteLine("foo" + (aa + b));
return this;
}
public static void bar()
{
Console.WriteLine("bar");
}
}
[Serializable]
[Obsolete]
[Test(true, TestProp = "1234")]
[Test]
public class TestCls2
{
[TestCLR]
public int Attribute_field;
[Test]
public int ILAttribute_field;
[TestCLR]
public int Attribute_prop { get; set; }
[Test]
public int ILAttribute_prop { get; set; }
}
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
class TestAttribute : Attribute
{
bool testField;
public TestAttribute()
{
}
public TestAttribute(bool test)
{
testField = test;
}
public string TestProp { get; set; }
}
public class MyClass
{
public uint B { get; set; }
}
public static void ReflectionTest09()
{
var r = new MyClass()
{
B = 14,
};
PropertyInfo p = typeof(MyClass).GetProperty("B");
object obj = p.GetGetMethod().Invoke(r, null);
Console.WriteLine("p type: " + obj.GetType().FullName); //显示System.Int32,正确应该是System.UInt32
if (obj is ValueType)
{
Console.WriteLine("value type");
}
else
{
throw new Exception("not value type"); //走进了这里
}
obj = p.GetValue(r, null);
Console.WriteLine("p type: " + obj.GetType().FullName); //显示System.Int32,正确应该是System.UInt32
if (obj is ValueType)
{
Console.WriteLine("value type");
}
else
{
throw new Exception("not value type"); //走进了这里
}
}
public class Tx
{
public float FloatField { get; set; }
public int IntField { get; set; }
public EnumTest.TestEnum EnumField { get; set; }
public static int StaticField { get; set; }
}
public static void ReflectionTest10()
{
Tx obj = new Tx { FloatField = 21, IntField = 21 };
Type t = obj.GetType();
var fields = t.GetProperties(BindingFlags.Public);
var info = fields[0]; //FloatField
object value = info.GetGetMethod().Invoke(obj, null);
Console.WriteLine(string.Format("{0} = {1}", info.Name, value));
if (value == null)
{
throw new Exception("null obj - FloatField"); // 不应该走到这里来,但走到了这里
}
info = fields[1];
value = info.GetGetMethod().Invoke(obj, null);
Console.WriteLine(string.Format("{0} = {1}", info.Name, value));
if (value == null)
{
throw new Exception("null obj - IntField");
}
else
{
Console.WriteLine("not null obj - IntField"); // 对于int是正确的,走到了这里
}
info = fields[2];
value = info.GetGetMethod().Invoke(obj, null);
Console.WriteLine(string.Format("{0} = {1}", info.Name, value));
info = fields[3];
value = info.GetValue(null, null);
Console.WriteLine(string.Format("{0} = {1}", info.Name, value));
}
class test24Class
{
public int this[int index, long index2]
{
get
{
return (int)(index + index2);
}
set
{
Console.WriteLine($"{index},{index2}={value}");
}
}
}
public static void ReflectionTest11()
{
ReflectionTest11Sub(new test24Class());
}
static void ReflectionTest11Sub(object o)
{
var p = o.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//error
foreach (var i in p)
{
Console.WriteLine(i.GetValue(o, new object[] { 1, 2L, 3333 }));
i.SetValue(o, 333, new object[] { 123, 345L, 678 });
}
}
public static void ReflectionTest12()
{
var types = ILRuntimeTest.TestMainForm._app.LoadedTypes.ToArray();
for (int i = 0; i < types.Length; i++)
{
Type type = types[i].Value.ReflectionType;
//if (type.BaseType != null && (type.BaseType == typeof(Attribute) || type.BaseType.Name == "Attribute"))
// continue;
//if (type.BaseType != null && type.BaseType.Name == "Void")
// continue;
object[] attrs = type.GetCustomAttributes(typeof(TestAttribute), false);
}
}
public static void ReflectionTest13()
{
object[] attrs = typeof(TestController).GetCustomAttributes(typeof(ObjectEventAttribute), false);
//结果attrs的Length > 0 , 这是错误的结果吧
Console.WriteLine(attrs.Length);
}
public static void ReflectionTest14()
{
TestTypeAssignableFrom(typeof(PlayerInfo));
}
class Ron<T>
{
public T Value { get; set; }
}
public static void ReflectionTest15()
{
Type t = typeof(Ron<>);
Console.WriteLine("t Type ==> " + t);
Type constructed = t.MakeGenericType(new Type[] { typeof(string) });
Ron<string> ins = Activator.CreateInstance(constructed, new object[] { "Test Success" }) as Ron<string>;
ins.Value = "OK";
Console.WriteLine(ins.Value);
}
private static void TestTypeAssignableFrom(Type targetType)
{
foreach (System.Reflection.PropertyInfo property in targetType.GetProperties())
{
if (!property.CanWrite)
{
continue;
}
Console.WriteLine(property.Name + "|" + typeof(System.Collections.ICollection).IsAssignableFrom(property.PropertyType));
}
foreach (System.Reflection.FieldInfo field in targetType.GetFields())
{
Console.WriteLine(field.Name + "|" + typeof(System.Collections.ICollection).IsAssignableFrom(field.FieldType));
}
}
class PlayerInfo
{
public string[] tags_F;
public Detail[] Details_F;
public string[] tags_P { get; set; }
public Detail[] Details_P { get; set; }
}
class Detail
{
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class ObjectEventAttribute : Attribute
{
}
[Test]
public sealed class TestController
{
public static TestController instance = new TestController();
}
public static void ReflectionTest16()
{
MethodInfo info = typeof(ReflectionTest).GetMethod(nameof(ReflectionTest16Sub), BindingFlags.NonPublic | BindingFlags.Static);
info.Invoke(null, new object[] { null });
info.Invoke(null, new object[] { null });
}
public static void ReflectionTest17()
{
MethodInfo info = typeof(ReflectionTest).GetMethod(nameof(ReflectionTest17Sub), BindingFlags.NonPublic | BindingFlags.Static);
int cnt = 0;
cnt += (int)info.Invoke(null, new object[] { null });
cnt += (int)info.Invoke(null, new object[] { null });
Console.WriteLine(cnt);
if (cnt != 40)
throw new Exception("Wrong result");
}
static void ReflectionTest16Sub(TestCases.TestCls t)
{
Console.WriteLine("OK");
}
static int ReflectionTest17Sub()
{
return 20;
}
}
}