forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignmentDefinition.cs
More file actions
187 lines (161 loc) · 6.28 KB
/
AssignmentDefinition.cs
File metadata and controls
187 lines (161 loc) · 6.28 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using System.Reflection;
using ServiceStack.Common.Utils;
using ServiceStack.Logging;
using ServiceStack.Text;
namespace ServiceStack.Common.Support
{
public class AssignmentEntry
{
public string Name;
public AssignmentMember From;
public AssignmentMember To;
public PropertyGetterDelegate GetValueFn;
public PropertySetterDelegate SetValueFn;
public AssignmentEntry(string name, AssignmentMember @from, AssignmentMember to)
{
Name = name;
From = @from;
To = to;
GetValueFn = From.GetGetValueFn();
SetValueFn = To.GetSetValueFn();
}
}
public class AssignmentMember
{
public AssignmentMember(Type type, PropertyInfo propertyInfo)
{
Type = type;
PropertyInfo = propertyInfo;
}
public AssignmentMember(Type type, FieldInfo fieldInfo)
{
Type = type;
FieldInfo = fieldInfo;
}
public AssignmentMember(Type type, MethodInfo methodInfo)
{
Type = type;
MethodInfo = methodInfo;
}
public Type Type;
public PropertyInfo PropertyInfo;
public FieldInfo FieldInfo;
public MethodInfo MethodInfo;
public PropertyGetterDelegate GetGetValueFn()
{
if (PropertyInfo != null)
return PropertyInfo.GetPropertyGetterFn();
if (FieldInfo != null)
return o => FieldInfo.GetValue(o);
if (MethodInfo != null)
return (PropertyGetterDelegate)
Delegate.CreateDelegate(typeof(PropertyGetterDelegate), MethodInfo);
return null;
}
public PropertySetterDelegate GetSetValueFn()
{
if (PropertyInfo != null)
return PropertyInfo.GetPropertySetterFn();
if (FieldInfo != null)
return (o, v) => FieldInfo.SetValue(o, v);
if (MethodInfo != null)
return (PropertySetterDelegate)
Delegate.CreateDelegate(typeof(PropertySetterDelegate), MethodInfo);
return null;
}
}
public class AssignmentDefinition
{
private static readonly ILog Log = LogManager.GetLogger(typeof(AssignmentDefinition));
public AssignmentDefinition()
{
this.AssignmentMemberMap = new Dictionary<string, AssignmentEntry>();
}
public Type FromType { get; set; }
public Type ToType { get; set; }
public Dictionary<string, AssignmentEntry> AssignmentMemberMap { get; set; }
public void AddMatch(string name, AssignmentMember readMember, AssignmentMember writeMember)
{
this.AssignmentMemberMap[name] = new AssignmentEntry(name, readMember, writeMember);
}
public void PopulateFromPropertiesWithAttribute(object to, object from, Type attributeType)
{
var hasAttributePredicate = (Func<PropertyInfo, bool>)
(x => x.GetCustomAttributes(attributeType, true).Length > 0);
Populate(to, from, hasAttributePredicate, null);
}
public void PopulateWithNonDefaultValues(object to, object from)
{
var nonDefaultPredicate = (Func<object, bool>) (x =>
x != null && !Equals( x, ReflectionUtils.GetDefaultValue(x.GetType()) )
);
Populate(to, from, null, nonDefaultPredicate);
}
public void Populate(object to, object from)
{
Populate(to, from, null, null);
}
public void Populate(object to, object from,
Func<PropertyInfo, bool> propertyInfoPredicate,
Func<object, bool> valuePredicate)
{
foreach (var assignmentEntry in AssignmentMemberMap)
{
var assignmentMember = assignmentEntry.Value;
var fromMember = assignmentEntry.Value.From;
var toMember = assignmentEntry.Value.To;
if (fromMember.PropertyInfo != null && propertyInfoPredicate != null)
{
if (!propertyInfoPredicate(fromMember.PropertyInfo)) continue;
}
try
{
var fromValue = assignmentMember.GetValueFn(from);
if (valuePredicate != null)
{
if (!valuePredicate(fromValue)) continue;
}
if (fromMember.Type != toMember.Type)
{
if (fromMember.Type == typeof(string))
{
fromValue = TypeSerializer.DeserializeFromString((string)fromValue, toMember.Type);
}
else if (toMember.Type == typeof(string))
{
fromValue = TypeSerializer.SerializeToString(fromValue);
}
else if (toMember.Type.IsGenericType
&& toMember.Type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Type genericArg = toMember.Type.GetGenericArguments()[0];
if (genericArg.IsEnum)
{
fromValue = Enum.ToObject(genericArg, fromValue);
}
}
else
{
var listResult = TranslateListWithElements.TryTranslateToGenericICollection(
fromMember.Type, toMember.Type, fromValue);
if (listResult != null)
{
fromValue = listResult;
}
}
}
var setterFn = assignmentMember.SetValueFn;
setterFn(to, fromValue);
}
catch (Exception ex)
{
Log.Warn(string.Format("Error trying to set properties {0}.{1} > {2}.{3}",
FromType.FullName, fromMember.Type.Name,
ToType.FullName, toMember.Type.Name), ex);
}
}
}
}
}