Skip to content

Instantly share code, notes, and snippets.

@pdoran
Created July 6, 2014 22:06
Show Gist options
  • Save pdoran/52087b9a9adf73fc7572 to your computer and use it in GitHub Desktop.
Save pdoran/52087b9a9adf73fc7572 to your computer and use it in GitHub Desktop.
CachedGenericReflection.cs
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