forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionTests.cs
More file actions
116 lines (107 loc) · 4.54 KB
/
ReflectionTests.cs
File metadata and controls
116 lines (107 loc) · 4.54 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
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
using System;
using System.Reflection;
using NUnit.Framework;
namespace UnitTests.Serialization
{
public class ReflectionTests
{
public class MyTypeClass
{
public String MyProperty1
{
get
{
return "hello";
}
}
public String MyProperty2
{
get
{
return "hello";
}
}
protected String MyProperty3
{
get
{
return "hello";
}
}
}
[Test]
public void Main()
{
Type myType = (typeof(MyTypeClass));
// Get the public properties.
PropertyInfo[] myPropertyInfo = myType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
System.Diagnostics.Trace.WriteLine($"The number of public properties is {myPropertyInfo.Length}.");
// Display the public properties.
DisplayPropertyInfo(myPropertyInfo);
// Get the nonpublic properties.
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance);
System.Diagnostics.Trace.WriteLine($"The number of protected properties is {myPropertyInfo1.Length}.");
// Display all the nonpublic properties.
DisplayPropertyInfo(myPropertyInfo1);
try
{
// Get Type object of MyClass.
myType = typeof(MyTypeClass);
// Get the PropertyInfo by passing the property name and specifying the BindingFlags.
PropertyInfo myPropInfo = myType.GetProperty("MyProperty1", BindingFlags.Public | BindingFlags.Instance);
// Display Name propety to System.Diagnostics.Trace.
System.Diagnostics.Trace.WriteLine("{0} is a property of MyClass.", myPropInfo.Name);
}
catch (NullReferenceException e)
{
System.Diagnostics.Trace.WriteLine("MyProperty does not exist in MyClass." + e.Message);
}
}
public static void DisplayPropertyInfo(PropertyInfo[] myPropertyInfo)
{
// Display information for all properties.
for (int i = 0; i < myPropertyInfo.Length; i++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i];
System.Diagnostics.Trace.WriteLine($"The property name is {myPropInfo.Name}." );
System.Diagnostics.Trace.WriteLine($"The property type is {myPropInfo.PropertyType}.");
}
}
public static object GetFieldValue(object item, string fieldName,
BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance)
{
var type = item.GetType();
return GetFieldValue(type, item, fieldName, flags);
}
private static object GetFieldValue(Type type, object item, string fieldName,
BindingFlags flags)
{
var fld = type.GetField(fieldName, flags);
if (fld == null)
{
if (type.BaseType != null)
return GetFieldValue(type.BaseType, fieldName, flags);
throw new ArgumentException("Unknown field or improper flags");
}
return fld.GetValue(item);
}
public static object GetPropertyValue(object item, string propertyName,
BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance)
{
var type = item.GetType();
return GetPropertyValue(type, item, propertyName, flags);
}
public static object GetPropertyValue(Type type, object item, string propertyName,
BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance)
{
var prp = type.GetProperty(propertyName, flags);
if (prp == null)
{
if (type.BaseType != null)
GetPropertyValue(type.BaseType, item, propertyName, flags);
throw new ArgumentException("Unknown field or improper flags");
}
return prp.GetValue(item, null);
}
}
}