forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConcreteTypeCreator.cs
More file actions
104 lines (92 loc) · 3.86 KB
/
ConcreteTypeCreator.cs
File metadata and controls
104 lines (92 loc) · 3.86 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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Simple.Data.Extensions;
using System.Collections;
namespace Simple.Data
{
internal class ConcreteTypeCreator
{
private static readonly ConcurrentDictionary<Type, ConcreteTypeCreator> Cache =
new ConcurrentDictionary<Type, ConcreteTypeCreator>();
private readonly Type _concreteType;
private ConcreteTypeCreator(Type concreteType)
{
_concreteType = concreteType;
}
public Type ConcreteType
{
get { return _concreteType; }
}
public static ConcreteTypeCreator Get(Type concreteType)
{
return Cache.GetOrAdd(concreteType, type => new ConcreteTypeCreator(type));
}
public bool TryCreate(IDictionary<string, object> data, out object result)
{
bool anyPropertiesSet = false;
object obj = Activator.CreateInstance(_concreteType);
object value;
foreach (var propertyInfo in _concreteType.GetProperties().Where(pi => CanSetProperty(pi, data)))
{
value = data[propertyInfo.Name];
if (ConcreteCollectionTypeCreator.IsCollectionType(propertyInfo.PropertyType))
{
if (!ConcreteCollectionTypeCreator.TryCreate(propertyInfo.PropertyType, (IEnumerable)value, out value))
continue;
}
else
{
var subData = value as IDictionary<string, object>;
if (subData != null && !ConcreteTypeCreator.Get(propertyInfo.PropertyType).TryCreate(subData, out value))
continue;
}
if (value != null && propertyInfo.PropertyType.IsEnum && value is string)
{
value = Enum.Parse(propertyInfo.PropertyType, value.ToString());
}
else if (value != null && IsTypeConversionRequired(value.GetType(), propertyInfo.PropertyType))
{
value = Convert.ChangeType(value, propertyInfo.PropertyType);
}
if (propertyInfo.CanWrite)
{
propertyInfo.SetValue(obj, value, null);
anyPropertiesSet = true;
}
else
{
var propertyValue = propertyInfo.GetValue(obj, null);
if (propertyValue == null) continue;
var addMethod = propertyValue.GetType().GetMethod("Add");
if (addMethod != null)
{
var valueItems = value as IEnumerable;
if (valueItems != null)
{
foreach (var valueItem in valueItems)
{
addMethod.Invoke(propertyValue, new[] {valueItem});
}
}
}
}
}
result = anyPropertiesSet ? obj : null;
return anyPropertiesSet;
}
private static bool IsTypeConversionRequired(Type source, Type target)
{
if (target.IsEnum) return !target.GetEnumUnderlyingType().IsAssignableFrom(source);
return !target.IsAssignableFrom(source);
}
private static bool CanSetProperty(PropertyInfo propertyInfo, IDictionary<string, object> data)
{
return data.ContainsKey(propertyInfo.Name) &&
!(propertyInfo.PropertyType.IsValueType && data[propertyInfo.Name] == null);
}
}
}