forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionUtility.cs
More file actions
169 lines (148 loc) · 7.36 KB
/
ReflectionUtility.cs
File metadata and controls
169 lines (148 loc) · 7.36 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
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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
namespace SharpMap.Utilities
{
internal static class ReflectionUtility
{
internal const BindingFlags PrivateInstance = BindingFlags.NonPublic | BindingFlags.Instance;
internal static void SetFieldValue<T>(ref object obj, string fieldName,
BindingFlags bindingFlags = PrivateInstance, T newValue = default(T))
{
var type = obj.GetType();
var field = SearchField(type, fieldName, bindingFlags);
var setObj = (bindingFlags & BindingFlags.Static) == BindingFlags.Static ? null : obj;
if (field != null)
field.SetValue(setObj, newValue);
else
System.Diagnostics.Trace.WriteLine(obj,
string.Format("could not get '{0}' from '{1}'", fieldName,
obj.GetType()));
}
internal static T GetFieldValue<T>(object obj, string fieldName, BindingFlags bindingFlags = PrivateInstance,
T defaultValue = default(T))
{
var type = obj.GetType();
var field = SearchField(type, fieldName, bindingFlags);
if (field != null)
return (T)field.GetValue(obj);
System.Diagnostics.Trace.WriteLine(obj,
string.Format("could not get '{0}' from '{1}'", fieldName, obj.GetType()));
return defaultValue;
}
internal static FieldInfo SearchField(Type type, string name, BindingFlags bindingFlags)
{
CheckBindingFlags(bindingFlags);
while (type != null)
{
var fi = type.GetField(name, bindingFlags);
if (fi != null)
return fi;
type = type.BaseType;
}
return null;
}
internal static PropertyInfo SearchProperty(Type type, string name, BindingFlags bindingFlags, bool set = false)
{
CheckBindingFlags(bindingFlags);
while (type != null)
{
var fi = type.GetProperty(name, bindingFlags);
if (fi != null)
{
if (set && !fi.CanWrite) fi = null;
if (fi != null) return fi;
}
type = type.BaseType;
}
return null;
}
// ReSharper disable UnusedParameter.Local
private static void CheckBindingFlags(BindingFlags bindingFlags)
// ReSharper restore UnusedParameter.Local
{
if ((bindingFlags & (BindingFlags.Public | BindingFlags.NonPublic)) == BindingFlags.Default)
throw new ArgumentException("Binding flags need to specify Public or NonPublic");
if ((bindingFlags & (BindingFlags.Static | BindingFlags.Instance)) == BindingFlags.Default)
throw new ArgumentException("Binding flags need to specify Static or Instance");
}
internal static void SetPropertyValue<T>(ref object obj, string propertyName,
BindingFlags bindingFlags = PrivateInstance, T newValue = default(T))
{
var type = obj.GetType();
var property = SearchProperty(type, propertyName, bindingFlags, true);
var setObj = (bindingFlags & BindingFlags.Static) == BindingFlags.Static ? null : obj;
if (property != null)
property.SetValue(setObj, newValue, null);
else
System.Diagnostics.Trace.WriteLine(obj,
string.Format("could not get '{0}' from '{1}'", propertyName,
obj.GetType()));
}
internal static T GetPropertyValue<T>(object obj, string propertyName,
BindingFlags bindingFlags = PrivateInstance, T newValue = default(T))
{
var type = obj.GetType();
var property = SearchProperty(type, propertyName, bindingFlags);
if (property != null)
return (T)property.GetValue(obj, null);
System.Diagnostics.Trace.WriteLine(obj,
string.Format("could not get '{0}' from '{1}'", propertyName,
obj.GetType()));
return newValue;
}
public static void GetList<T>(IList<T> obj, SerializationInfo info, StreamingContext context,
string baseFieldName)
{
info.AddValue(baseFieldName + "IsNull", obj == null);
if (obj == null) return;
info.AddValue(baseFieldName + "Type", obj.GetType());
info.AddValue(baseFieldName + "Count", obj.Count);
for (var i = 0; i < obj.Count; i++)
{
info.AddValue(baseFieldName + i, obj[i]);
}
}
public static IList<T> SetList<T>(SerializationInfo info, StreamingContext context, string baseFieldName)
{
if (info.GetBoolean(baseFieldName + "IsNull"))
return null;
var type = (Type)info.GetValue(baseFieldName + "Type", typeof(Type));
var instance = (IList<T>)Activator.CreateInstance(type);
var count = info.GetInt32(baseFieldName + "Count");
for (var i = 0; i < count; i++)
instance.Add((T)info.GetValue(baseFieldName + i, typeof(T)));
return instance;
}
public static void GetDictionary<TKey, TValue>(IDictionary<TKey, TValue> obj, SerializationInfo info, StreamingContext context,
string baseFieldName)
{
info.AddValue(baseFieldName + "IsNull", obj == null);
if (obj == null) return;
info.AddValue(baseFieldName + "Type", obj.GetType());
info.AddValue(baseFieldName + "Count", obj.Count);
var i = 0;
foreach (var kvp in obj)
{
info.AddValue(baseFieldName + "Key" + i, kvp.Key);
info.AddValue(baseFieldName + "Value" + i++, kvp.Value);
}
}
public static IDictionary<TKey, TValue> SetDictionary<TKey, TValue>(SerializationInfo info, StreamingContext context, string baseFieldName)
{
if (info.GetBoolean(baseFieldName + "IsNull"))
return null;
var type = (Type)info.GetValue(baseFieldName + "Type", typeof(Type));
var instance = (IDictionary<TKey, TValue>)Activator.CreateInstance(type);
var count = info.GetInt32(baseFieldName + "Count");
for (var i = 0; i < count; i++)
{
var key = (TKey)info.GetValue(baseFieldName + "Key" + i, typeof(TKey));
var value = (TValue)info.GetValue(baseFieldName + "Value" + i, typeof(TValue));
instance.Add(key, value);
}
return instance;
}
}
}