Created
July 6, 2014 22:06
-
-
Save pdoran/52087b9a9adf73fc7572 to your computer and use it in GitHub Desktop.
CachedGenericReflection.cs
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
private Dictionary<Type, ConstructorInfo> _CachedLookup; | |
private object GenericMessageCreationFromCache(object entity) { | |
Type entityType = entity.GetType(); | |
ConstructorInfo genericConstructor = null; | |
if (!_CachedLookup.TryGetValue(entityType, out genericConstructor)) { | |
Type genericType = typeof(Message<>); | |
Type specificType = genericType.MakeGenericType(entityType); | |
Type[] constructorParams = new[] { entityType }; | |
genericConstructor = specificType.GetConstructor(constructorParams); | |
_CachedLookup.Add(entityType, genericConstructor); | |
} | |
return genericConstructor.Invoke(new object[] { entity }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment