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
109 lines (92 loc) · 3.71 KB
/
ConcreteTypeCreator.cs
File metadata and controls
109 lines (92 loc) · 3.71 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
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
{
using System.Diagnostics;
using System.Linq.Expressions;
using System.Threading;
internal class ConcreteTypeCreator
{
private readonly Lazy<Func<IDictionary<string, object>, object>> _func;
private static readonly Dictionary<Type, ConcreteTypeCreator> Creators;
private static readonly ICollection CreatorsCollection;
static ConcreteTypeCreator()
{
CreatorsCollection = Creators = new Dictionary<Type, ConcreteTypeCreator>();
}
private ConcreteTypeCreator(Lazy<Func<IDictionary<string, object>, object>> func)
{
_func = func;
}
public object Create(IDictionary<string, object> source)
{
var func = _func.Value;
return func(source);
}
public bool TryCreate(IDictionary<string,object> source, out object result)
{
try
{
result = Create(source);
return true;
}
catch (Exception)
{
result = null;
return false;
}
}
public static ConcreteTypeCreator Get(Type targetType)
{
if (CreatorsCollection.IsSynchronized && Creators.ContainsKey(targetType))
{
return Creators[targetType];
}
lock (CreatorsCollection.SyncRoot)
{
if (Creators.ContainsKey(targetType)) return Creators[targetType];
var creator = BuildCreator(targetType);
Creators.Add(targetType, creator);
return creator;
}
}
private static ConcreteTypeCreator BuildCreator(Type targetType)
{
var creator = new ConcreteTypeCreator(new Lazy<Func<IDictionary<string, object>, object>>(() => BuildLambda(targetType), LazyThreadSafetyMode.PublicationOnly));
return creator;
}
private static Func<IDictionary<string, object>, object> BuildLambda(Type targetType)
{
var param = Expression.Parameter(typeof (IDictionary<string, object>), "source");
var obj = Expression.Variable(targetType, "obj");
var create = CreateNew(targetType, obj);
var assignments = Expression.Block(
targetType.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Where(PropertyIsConvertible)
.Select(p => new PropertySetterBuilder(param, obj, p).CreatePropertySetter()));
var block = Expression.Block(new[] {obj},
create,
assignments,
obj);
var lambda = Expression.Lambda<Func<IDictionary<string, object>, object>>(block, param).Compile();
return lambda;
}
private static bool PropertyIsConvertible(PropertyInfo property)
{
return property.CanWrite || property.PropertyType.IsGenericCollection();
}
private static BinaryExpression CreateNew(Type targetType, ParameterExpression obj)
{
var ctor = targetType.GetConstructor(Type.EmptyTypes);
Debug.Assert(ctor != null);
var create = Expression.Assign(obj, Expression.New(ctor)); // obj = new T();
return create;
}
}
}