-
Notifications
You must be signed in to change notification settings - Fork 3
/
ShallowCloner.cs
151 lines (119 loc) · 5.08 KB
/
ShallowCloner.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
using System;
using System.Linq;
using System.Reflection;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Dynamic;
using System.Collections;
using System.ComponentModel;
using Reflection = Ramda.NET.ReflectionExtensions;
namespace Ramda.NET
{
internal static class ShallowCloner
{
private static Dictionary<Type, Delegate> cache = new Dictionary<Type, Delegate>();
internal static object Clone(this object source) {
var type = source.GetType();
if (type.IsAnonymousType()) {
return source.ToExpando();
}
else {
var @delegate = cache.GetOrAdd(type, () => {
var parameter = Expression.Parameter(type);
var clone = Expression.Call(parameter, type.GetMethod("MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic));
var lambda = Expression.Lambda(
Expression.GetFuncType(new[] { type, type }),
Expression.Convert(clone, type), parameter);
return lambda.Compile();
});
return @delegate.DynamicInvoke(source);
}
}
internal static object CloneAndAssignValue(dynamic prop, object propValue, object obj) {
object target = null;
var type = obj.GetType();
if (type.IsAnonymousType()) {
target = AnonymousTypeCloneAndAssignValue(prop, propValue, type, obj);
}
else {
MemberInfo memberInfo = Reflection.TryGetMemberInfoFromType(type, prop);
if (memberInfo != null && memberInfo.GetUnderlyingType().IsAssignableFrom(propValue.GetType())) {
target = WellKnownTypeCloneAndAssignValue(prop, obj, propValue);
}
else {
IDictionary<string, object> expando = Reflection.ToExpando(obj);
expando[prop] = propValue;
target = expando;
}
}
return target;
}
internal static object CloneAndOmitValue(string prop, object obj) {
IDictionary<string, object> expando = obj.ToExpando();
expando.Remove(prop);
return expando as ExpandoObject;
}
private static object AnonymousTypeCloneAndAssignValue(object prop, object propValue, Type type, object obj) {
var propHasBeenSet = false;
bool shouldCreateExpando = false;
var arguments = new List<object>();
var ctor = type.GetConstructors()[0];
var tuples = ctor.GetParameters().Select((param, i) => {
object result = null;
var paramType = param.ParameterType;
if (param.Name.Equals(prop)) {
propHasBeenSet = true;
result = propValue;
}
else {
result = obj.Member(param.Name);
}
shouldCreateExpando |= !paramType.Equals(result.GetType());
return new {
Value = result,
Name = param.Name
};
}).ToList();
if (shouldCreateExpando || !propHasBeenSet) {
IDictionary<string, object> expando = new ExpandoObject();
tuples.ForEach(tuple => expando[tuple.Name] = tuple.Value);
expando[prop.ToString()] = propValue;
return expando;
}
arguments.AddRange(tuples.Select(tuple => tuple.Value));
return ctor.Invoke(arguments.ToArray());
}
private static object WellKnownTypeCloneAndAssignValue(object prop, object obj, object value) {
var target = obj.Clone();
if (target.IsList() && prop.GetType().Equals(typeof(int))) {
var index = (int)prop;
var list = target as IList;
list[index] = value;
}
else if (target.IsExpandoObject()) {
var expando = target as IDictionary<string, object>;
expando[prop.ToString()] = value;
}
else {
var member = obj.TryGetMemberInfo(prop.ToString());
if (member.IsNotNull()) {
switch (member.MemberType) {
case MemberTypes.Property:
var property = (PropertyInfo)member;
if (property.CanWrite) {
property.SetValue(target, value, null);
}
break;
case MemberTypes.Field:
var field = (FieldInfo)member;
if (!field.IsInitOnly) {
field.SetValue(target, value);
}
break;
}
}
}
return target;
}
}
}