|
9 | 9 |
|
10 | 10 | namespace Enyim.Reflection
|
11 | 11 | {
|
12 |
| - /// <summary> |
13 |
| - /// <para>Implements a very fast object factory for dynamic object creation. Dynamically generates a factory class which will use the new() constructor of the requested type.</para> |
14 |
| - /// <para>Much faster than using Activator at the price of the first invocation being significantly slower than subsequent calls.</para> |
15 |
| - /// </summary> |
16 |
| - public static class FastActivator |
17 |
| - { |
18 |
| - private static Dictionary<Type, Func<object>> factoryCache = new Dictionary<Type, Func<object>>(); |
| 12 | + /// <summary> |
| 13 | + /// <para>Implements a very fast object factory for dynamic object creation. Dynamically generates a factory class which will use the new() constructor of the requested type.</para> |
| 14 | + /// <para>Much faster than using Activator at the price of the first invocation being significantly slower than subsequent calls.</para> |
| 15 | + /// </summary> |
| 16 | + public static class FastActivator |
| 17 | + { |
| 18 | + private static Dictionary<Type, Func<object>> factoryCache = new Dictionary<Type, Func<object>>(); |
19 | 19 |
|
20 |
| - /// <summary> |
21 |
| - /// Creates an instance of the specified type using a generated factory to avoid using Reflection. |
22 |
| - /// </summary> |
23 |
| - /// <typeparam name="T">The type to be created.</typeparam> |
24 |
| - /// <returns>The newly created instance.</returns> |
25 |
| - public static T Create<T>() |
26 |
| - { |
27 |
| - return TypeFactory<T>.Create(); |
28 |
| - } |
| 20 | + /// <summary> |
| 21 | + /// Creates an instance of the specified type using a generated factory to avoid using Reflection. |
| 22 | + /// </summary> |
| 23 | + /// <typeparam name="T">The type to be created.</typeparam> |
| 24 | + /// <returns>The newly created instance.</returns> |
| 25 | + public static T Create<T>() |
| 26 | + { |
| 27 | + return TypeFactory<T>.Create(); |
| 28 | + } |
29 | 29 |
|
30 |
| - /// <summary> |
31 |
| - /// Creates an instance of the specified type using a generated factory to avoid using Reflection. |
32 |
| - /// </summary> |
33 |
| - /// <param name="type">The type to be created.</param> |
34 |
| - /// <returns>The newly created instance.</returns> |
35 |
| - public static object Create(Type type) |
36 |
| - { |
37 |
| - Func<object> f; |
| 30 | + /// <summary> |
| 31 | + /// Creates an instance of the specified type using a generated factory to avoid using Reflection. |
| 32 | + /// </summary> |
| 33 | + /// <param name="type">The type to be created.</param> |
| 34 | + /// <returns>The newly created instance.</returns> |
| 35 | + public static object Create(Type type) |
| 36 | + { |
| 37 | + Func<object> f; |
38 | 38 |
|
39 |
| - if (!factoryCache.TryGetValue(type, out f)) |
40 |
| - lock (factoryCache) |
41 |
| - if (!factoryCache.TryGetValue(type, out f)) |
42 |
| - { |
43 |
| - factoryCache[type] = f = Expression.Lambda<Func<object>>(Expression.New(type)).Compile(); |
44 |
| - } |
| 39 | + if (!factoryCache.TryGetValue(type, out f)) |
| 40 | + { |
| 41 | + lock (factoryCache) |
| 42 | + if (!factoryCache.TryGetValue(type, out f)) |
| 43 | + { |
| 44 | + f = Expression.Lambda<Func<object>>(Expression.New(type)).Compile(); |
| 45 | + if (f != null) |
| 46 | + { |
| 47 | + factoryCache[type] = f; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
45 | 51 |
|
46 |
| - return f(); |
47 |
| - } |
| 52 | + return f(); |
| 53 | + } |
48 | 54 |
|
49 |
| - private static class TypeFactory<T> |
50 |
| - { |
51 |
| - public static readonly Func<T> Create = Expression.Lambda<Func<T>>(Expression.New(typeof(T))).Compile(); |
52 |
| - } |
53 |
| - } |
| 55 | + private static class TypeFactory<T> |
| 56 | + { |
| 57 | + public static readonly Func<T> Create = Expression.Lambda<Func<T>>(Expression.New(typeof(T))).Compile(); |
| 58 | + } |
| 59 | + } |
54 | 60 | }
|
55 | 61 |
|
56 | 62 | #region [ License information ]
|
|
0 commit comments