I create a generic method:
private void UpdateAllProperties<idType, entityType>(entityType currentEntity, entityType newEntity)
where idType : IEquatable<idType>
where entityType : AbstractEntity<idType>
{
var currentEntityProperties = currentEntity.GetType().GetProperties();
var newEntityProperties = newEntity.GetType().GetProperties();
foreach (var currentEntityProperty in currentEntityProperties)
{
foreach (var newEntityProperty in newEntityProperties)
{
if (newEntityProperty.Name == currentEntityProperty.Name)
{
if (currentEntityProperty.PropertyType == typeof(AbstractEntity<>))
{
// Here i want to use this method again, but i need to inform the types.. how can i do anything like that:
var idPropertyType = currentEntityProperty.PropertyType.GetProperty("Id").GetType();
var entityPropertyType = currentEntityProperty.PropertyType;
// Here i got the error because i cannot set through this way
this.UpdateAllProperties<idPropertyType, entityPropertyType>(currentEntityProperty.GetValue(currentEntity, null), newEntityProperty.GetValue(newEntity, null));
break;
}
else if (currentEntityProperty.PropertyType == typeof(ICollection<>))
{
// TODO
break;
}
else
{
currentEntityProperty.SetValue(currentEntity, newEntityProperty.GetValue(newEntity, null), null);
break;
}
}
}
}
}
How can i do this?
You will have to make the call using
Type.GetMethod()to get the method andMethodInfo.MakeGenericMethod()to set it up with the right types, before usingMethodInfo.Invoke()to call the method.Something like: