-
Notifications
You must be signed in to change notification settings - Fork 0
/
FieldAndPropertyWrapper.cs
279 lines (233 loc) · 11.9 KB
/
FieldAndPropertyWrapper.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
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace FieldAndPropertyWrapper
{
/// <summary>
/// Wrapper to allow uniform access to <see cref="T:System.Reflection.FieldInfo"/> and <see cref="T:System.Reflection.PropertyInfo"/>.
/// Designed to be used with casts, rather than constructors.
/// </summary>
public sealed class FieldAndPropertyWrapper : IDisposable, IEquatable<FieldAndPropertyWrapper>
{
/// <summary>
/// Creates a new <see cref="FieldAndPropertyWrapper"/>
/// </summary>
/// <param name="prop">The <see cref="PropertyInfo"/> to wrap</param>
/// <seealso cref="FieldAndPropertyWrapper(FieldInfo)"/>
[UsedImplicitly]
private FieldAndPropertyWrapper(PropertyInfo prop)
{
if (prop == null) throw new ArgumentNullException(nameof(prop));
this._member = prop;
this._getter = o => prop.GetValue(o, null);
this._setter = (o, v) => prop.SetValue(o, v, null);
this._memberType = prop.PropertyType;
_Cache[prop] = this;
}
/// <summary>
/// Creates a new <see cref="FieldAndPropertyWrapper"/>
/// </summary>
/// <param name="field">The <see cref="FieldInfo"/> to wrap</param>
/// <seealso cref="FieldAndPropertyWrapper(PropertyInfo)"/>
[UsedImplicitly]
private FieldAndPropertyWrapper(FieldInfo field)
{
if (field == null) throw new ArgumentNullException(nameof(field));
this._member = field;
this._getter = field.GetValue;
this._setter = field.SetValue;
this._memberType = field.FieldType;
_Cache[field] = this;
}
/// <value>
/// The <see cref="T:System.Type"/> that declares the member the <see cref="T:CentipedeInterfaces.FieldAndPropertyWrapper"/> is referencing
/// </value>
public Type DeclaringType => this._member.DeclaringType;
private readonly MemberInfo _member;
private readonly Func<object, object> _getter;
/// <summary>
/// Gets the value of the wrapped member
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="o">The instance to get the value of the member of</param>
/// <returns></returns>
public T Get<T>(object o) => (T)this._getter(o);
private readonly Action<object, object> _setter;
private readonly Type _memberType;
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="o"></param>
/// <param name="value"></param>
public void Set<T>(object o, T value) => this._setter(o, value);
/// <summary>
///
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetAttribute<T>() => this._member.GetCustomAttributes(typeof(T), true).Cast<T>();
/// <summary>
///
/// </summary>
public string Name => this._member.Name;
/// <summary>
///
/// </summary>
public Type MemberType => this._memberType;
/// <summary>
///
/// </summary>
public enum FieldType
{
//first value listed is used for default()
/// <summary>
///
/// </summary>
Other,
/// <summary>
/// The wrapped member is a String type (string, char, etc)
/// </summary>
String,
/// <summary>
/// Wrapped member is a numeric type (int, double, Decimal, etc)
/// </summary>
Numeric,
/// <summary>
/// Wrapped member is boolean
/// </summary>
Boolean,
}
private static readonly Dictionary<Type, FieldType> _TypeMapping = new Dictionary<Type, FieldType>
{
{ typeof (bool), FieldType.Boolean },
{ typeof (byte), FieldType.Numeric },
{ typeof (sbyte), FieldType.Numeric },
{ typeof (char), FieldType.String },
{ typeof (decimal),FieldType.Numeric },
{ typeof (double), FieldType.Numeric },
{ typeof (float), FieldType.Numeric },
{ typeof (int), FieldType.Numeric },
{ typeof (uint), FieldType.Numeric },
{ typeof (long), FieldType.Numeric },
{ typeof (ulong), FieldType.Numeric },
{ typeof (short), FieldType.Numeric },
{ typeof (ushort), FieldType.Numeric },
{ typeof (string), FieldType.String },
};
/// <summary>
///
/// </summary>
/// <returns></returns>
public FieldType GetFieldTypeCategory()
{
Type baseType = MemberType;
while (!_TypeMapping.ContainsKey(baseType) && baseType != typeof(object))
{
baseType = baseType.BaseType ?? typeof(Object);
}
return _TypeMapping.TryGetValue(baseType, out FieldType fieldType) ? fieldType : FieldType.Other;
}
/// <summary>
/// Cast <see cref="FieldInfo"/> into <see cref="FieldAndPropertyWrapper"/>
/// </summary>
/// <param name="f"><see cref="FieldInfo"/> to wrap.</param>
/// <returns><see cref="FieldAndPropertyWrapper"/> wrapping <paramref name="f"/>.</returns>
public static implicit operator FieldAndPropertyWrapper(FieldInfo f) => _Cache.ContainsKey(f) ? _Cache[f] : new FieldAndPropertyWrapper(f);
/// <summary>
/// Unwrap <see cref="FieldAndPropertyWrapper"/> to <see cref="FieldInfo"/>
/// </summary>
/// <param name="wrapped"><see cref="FieldAndPropertyWrapper"/> to unwrap</param>
/// <returns>The <see cref="FieldInfo"/> <paramref name="wrapped"/> is wrapping</returns>
/// /// <exception cref="InvalidCastException"><paramref name="wrapped"/> is not wrapping a <see cref="FieldInfo"/>.</exception>
public static explicit operator FieldInfo(FieldAndPropertyWrapper wrapped) => wrapped._member is FieldInfo f ? f : throw new InvalidCastException();
/// <summary>
/// Cast <see cref="FieldInfo"/> into <see cref="FieldAndPropertyWrapper"/>
/// </summary>
/// <param name="p"><see cref="PropertyInfo"/> to wrap.</param>
/// <returns><see cref="FieldAndPropertyWrapper"/> wrapping <paramref name="p"/>.</returns>
public static implicit operator FieldAndPropertyWrapper(PropertyInfo p) => _Cache.ContainsKey(p) ? _Cache[p] : new FieldAndPropertyWrapper(p);
/// <summary>
/// Unwrap <see cref="FieldAndPropertyWrapper"/> to <see cref="PropertyInfo"/>
/// </summary>
/// <param name="wrapped"><see cref="FieldAndPropertyWrapper"/> to unwrap</param>
/// <returns>The <see cref="PropertyInfo"/> <paramref name="wrapped"/> is wrapping</returns>
/// <exception cref="InvalidCastException"><paramref name="wrapped"/> is not wrapping a <see cref="PropertyInfo"/>.</exception>
public static explicit operator PropertyInfo(FieldAndPropertyWrapper wrapped) => wrapped._member is PropertyInfo p ? p : throw new InvalidCastException();
private static readonly Dictionary<MemberInfo, FieldAndPropertyWrapper> _Cache = new Dictionary<MemberInfo, FieldAndPropertyWrapper>();
/// <summary>
/// Wrap <see cref="MemberInfo"/> in <see cref="FieldAndPropertyWrapper"/>
/// </summary>
/// <param name="m">The <see cref="MemberInfo"/> to wrap</param>
/// <returns>The wrapped <see cref="MemberInfo"/>.</returns>
/// <exception cref="InvalidCastException"><paramref name="m"/> is not an instance of <see cref="FieldInfo"/>
/// or <see cref="PropertyInfo"/></exception>
public static explicit operator FieldAndPropertyWrapper(MemberInfo m)
{
if (_Cache.ContainsKey(m))
{
Debug.WriteLine(@"FieldAndPropertyWrapper Cache hit {0}", m);
return _Cache[m];
}
Debug.WriteLine(@"FieldAndPropertyWrapper Cache miss {0}", m);
switch (m)
{
case FieldInfo field:
return new FieldAndPropertyWrapper(field);
case PropertyInfo prop:
return new FieldAndPropertyWrapper(prop);
default:
throw new InvalidCastException();
}
}
/// <summary>
/// Unwrap the wrapped <see cref="MemberInfo"/>
/// </summary>
/// <param name="wrapped">The <see cref="FieldAndPropertyWrapper"/> to unwrap</param>
/// <returns>The unwrapped <see cref="MemberInfo"/>.</returns>
public static implicit operator MemberInfo(FieldAndPropertyWrapper wrapped) => wrapped._member;
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
void IDisposable.Dispose() { }
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
// This won't work if you want to compare methods or constructors (due to the possiblity of
// different signatures, but then this class isn't intended to wrap those.
public bool Equals(FieldAndPropertyWrapper other) => DeclaringType == other.DeclaringType
&& Name == other.Name;
public static void SetPropertyOnObject<TObject, TValue>(TObject @object, Expression<Func<TObject, TValue>> propertySelector, TValue value)
{
if (@object == null) throw new ArgumentNullException(nameof(@object));
if (propertySelector.Body is MemberExpression memberAccess)
{
FieldAndPropertyWrapper property = (FieldAndPropertyWrapper)memberAccess.Member;
property.Set(@object, value);
}
else
{
throw new ArgumentException(nameof(propertySelector));
}
}
public static TValue GetPropertyOnObject<TObject, TValue>(TObject @object, Expression<Func<TObject, TValue>> propertySelector)
{
if (propertySelector.Body is MemberExpression memberAccess)
{
FieldAndPropertyWrapper property = (FieldAndPropertyWrapper)memberAccess.Member;
return property.Get<TValue>(@object);
}
else
{
throw new ArgumentException(nameof(propertySelector));
}
}
}
}