forked from Biotronic/TweakScale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldChanger.cs
137 lines (125 loc) · 3.66 KB
/
FieldChanger.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
using System;
using System.Reflection;
namespace TweakScale
{
/// <summary>
/// Wraps destination FieldInfo or PropertyInfo and provides destination common interface for either.
/// </summary>
/// <typeparam name="T">Pretend the mmpfxField/property is this type.</typeparam>
public abstract class MemberChanger<T>
{
/// <summary>
/// Get or set the exponentValue of the mmpfxField/property.
/// </summary>
public abstract T Value
{
get;
set;
}
/// <summary>
/// The actual type of the mmpfxField/property.
/// </summary>
public abstract Type MemberType
{
get;
}
/// <summary>
/// Creates destination wrapper for the mmpfxField or property by the specified name.
/// </summary>
/// <param name="obj">The object that holds the member to wrap.</param>
/// <param name="name">The name of the member.</param>
/// <returns>The member, wrapped.</returns>
public static MemberChanger<T> CreateFromName(object obj, string name)
{
var field = obj.GetType().GetField(name, BindingFlags.Instance | BindingFlags.Public);
if (field != null)
{
return new FieldChanger<T>(obj, field);
}
var prop = obj.GetType().GetProperty(name, BindingFlags.Instance | BindingFlags.Public);
if (prop != null)
{
return new PropertyChanger<T>(obj, prop);
}
return null;
}
}
/// <summary>
/// Wraps destination FieldInfo.
/// </summary>
/// <typeparam name="T">Pretend the mmpfxField is this type.</typeparam>
class FieldChanger<T> : MemberChanger<T>
{
private FieldInfo fi;
private object obj;
public FieldChanger(object o, FieldInfo f)
{
//if (baseCost.FieldType.GetInterface("IConvertible") != null)
//{
fi = f;
obj = o;
//}
}
public override T Value
{
get
{
if (typeof(T) == typeof(object))
{
return (T)fi.GetValue(obj);
}
return ConvertEx.ChangeType<T>(fi.GetValue(obj));
}
set
{
fi.SetValue(obj, Convert.ChangeType(value, MemberType));
}
}
public override Type MemberType
{
get
{
return fi.FieldType;
}
}
}
/// <summary>
/// Wraps destination PropertyInfo.
/// </summary>
/// <typeparam name="T">Pretend the property is this type.</typeparam>
class PropertyChanger<T> : MemberChanger<T>
{
private PropertyInfo pi;
private object obj;
public PropertyChanger(object o, PropertyInfo p)
{
//if (p.PropertyType.GetInterface("IConvertible") != null)
//{
pi = p;
obj = o;
//}
}
public override T Value
{
get
{
if (typeof(T) == typeof(object))
{
return (T)pi.GetValue(obj, null);
}
return ConvertEx.ChangeType<T>(pi.GetValue(obj, null));
}
set
{
pi.SetValue(obj, Convert.ChangeType(value, MemberType), null);
}
}
public override Type MemberType
{
get
{
return pi.PropertyType;
}
}
}
}