-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldGetValue.cs
More file actions
70 lines (57 loc) · 2.02 KB
/
Copy pathFieldGetValue.cs
File metadata and controls
70 lines (57 loc) · 2.02 KB
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
using System;
using System.Reflection;
class Program {
static void PrintFieldValues (FieldInfo[] fields, object obj) {
foreach (FieldInfo field in fields) {
// not printing field names because automatic property backing field names differ between .NET and JSIL
Console.WriteLine(field.GetValue(obj));
}
}
static void AssertThrows (Action action) {
try {
action();
Console.WriteLine("Not OK: exception was not thrown");
} catch (Exception) {
Console.WriteLine("OK: exception was thrown");
}
}
public static void Main () {
BindingFlags all = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
BindingFlags allStatic = all ^ BindingFlags.Instance;
PrintFieldValues(typeof(MyStruct).GetFields(all), new MyStruct(1, 2, "3"));
PrintFieldValues(typeof(MyEnum).GetFields(allStatic), null);
PrintFieldValues(typeof(MyClass).GetFields(all), new MyClass());
PrintFieldValues(typeof(MyClass).GetFields(all), new MySubclass());
PrintFieldValues(typeof(MyClass).GetFields(allStatic), null);
AssertThrows(() => PrintFieldValues(typeof(MyClass).GetFields(all), null));
AssertThrows(() => PrintFieldValues(typeof(MyStruct).GetFields(all), new MyClass()));
}
}
struct MyStruct {
public int Field1;
public long Field2;
public string Field3;
public MyStruct (byte field1, int field2, string field3) {
Field1 = field1;
Field2 = field2;
Field3 = field3;
}
}
enum MyEnum {
A = 3,
B = 5,
C = 7
}
class MyClass {
public int Field1 = 4;
public long Field2 = 8;
public string Field3 = "15";
public static uint StaticField1 = 16;
public static ulong StaticField2 = 23;
public static string AutomaticProperty1 { get; set; }
static MyClass() {
AutomaticProperty1 = "42";
}
}
class MySubclass : MyClass {
}