Skip to content

Instantly share code, notes, and snippets.

@pdoran
Last active August 29, 2015 14:03
Show Gist options
  • Save pdoran/ce39f7a23e0a94592530 to your computer and use it in GitHub Desktop.
Save pdoran/ce39f7a23e0a94592530 to your computer and use it in GitHub Desktop.
Generic Reflection
public void OnSave(object entity,
object id,
object[] state,
string[] propertyNames,
IType[] types)
{
Type entityType = entity.GetType();
Type genericType = typeof(Message<>);
Type specificType = genericType.MakeGenericType(entityType);
Type[] constructorParams = new[] { entityType };
ConstructorInfo genericConstructor = specificType.GetConstructor(constructorParams);
var message = genericConstructor.Invoke(new object[] { entity });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment