Skip to content

Commit 7ee81fa

Browse files
committed
Tweak FastActivator
1 parent 80e479f commit 7ee81fa

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

Enyim.Caching/FastActivator.cs

+43-37
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,54 @@
99

1010
namespace Enyim.Reflection
1111
{
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>>();
1919

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+
}
2929

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;
3838

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+
}
4551

46-
return f();
47-
}
52+
return f();
53+
}
4854

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+
}
5460
}
5561

5662
#region [ License information ]

0 commit comments

Comments
 (0)