forked from MapsterMapper/Mapster
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
53 additions
and
362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection.Emit; | ||
|
||
namespace Fpr.Utils | ||
{ | ||
public static class FastObjectFactory | ||
{ | ||
private static readonly object _syncLock = new object(); | ||
private static readonly Dictionary<Type, object> _creatorCache = new Dictionary<Type, object>(); | ||
|
||
public static void ClearObjectFactory<T>() where T : class | ||
{ | ||
_creatorCache.Remove(typeof (T)); | ||
} | ||
|
||
/// <summary> | ||
/// Create a new instance of the specified type | ||
/// </summary> | ||
/// <returns></returns> | ||
public static Func<T> CreateObjectFactory<T>(Func<T> factory = null) | ||
public static Func<T> CreateObjectFactory<T>(Func<T> factory = null) | ||
{ | ||
Type type = typeof(T); | ||
Type type = typeof (T); | ||
object createDelegate; | ||
if (!_creatorCache.TryGetValue(type, out createDelegate)) | ||
|
||
if (factory != null) | ||
{ | ||
createDelegate = factory; | ||
} | ||
else | ||
{ | ||
lock (_syncLock) | ||
{ | ||
if (!_creatorCache.TryGetValue(type, out createDelegate)) | ||
{ | ||
if (factory != null) | ||
{ | ||
createDelegate = factory; | ||
} | ||
else | ||
{ | ||
var dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + type.Name, type, null, type); | ||
ILGenerator ilGen = dynMethod.GetILGenerator(); | ||
var dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + type.Name, type, null, type); | ||
ILGenerator ilGen = dynMethod.GetILGenerator(); | ||
|
||
ilGen.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes)); | ||
ilGen.Emit(OpCodes.Ret); | ||
createDelegate = dynMethod.CreateDelegate(typeof(Func<T>)); | ||
} | ||
_creatorCache.Add(type, createDelegate); | ||
} | ||
} | ||
ilGen.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes)); | ||
ilGen.Emit(OpCodes.Ret); | ||
createDelegate = dynMethod.CreateDelegate(typeof (Func<T>)); | ||
} | ||
return (Func<T>)createDelegate; | ||
return (Func<T>) createDelegate; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.