Last active
August 29, 2015 14:03
-
-
Save pdoran/ce39f7a23e0a94592530 to your computer and use it in GitHub Desktop.
Generic Reflection
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
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