1 public class InstanceCachesEx
2 {
3 private Dictionary<Type, Func<object>> dicEx = new Dictionary<Type, Func<object>>();
4 public object InstanceCache(Type key)
5 {
6
7 object value = null;
8
9 if (dicEx.TryGetValue(key, out value))
10 {
11 return value();
12 }
13 else
14 {
15 value = CreateInstance(key);
16 dicEx[key] = value;
17 return value();
18 }
19 }
20
21 static Func<object> CreateInstance(Type type)
22 {
23 NewExpression newExp = Expression.New(type);
24 Expression<Func<object>> lambdaExp = Expression.Lambda<Func<object>>(newExp, null);
25 Func<object> func = lambdaExp.Compile();
26 return func;
27 }
28 }
29
30 public static class GetCache
31 {
32 static GetCache()
33 {
34 InstanceCacheEx = new InstanceCachesEx();
35 }
36
37 public static InstanceCachesEx InstanceCacheEx { get; set; }
38 }