The problem is Expression<Func<TViewModel, TResult>>
has absolutely no relation to PropertyInfo
(the type returned by GetProperties
). You'll also run into problems because the type of the result is not known at compile time.
The easiest solution would be to change ValidateProperty
to accept a PropertyInfo
:
protected virtual void ValidateProperty(PropertyInfo property, object value);
You could also convert the PropertyInfo
to an Expression
, but that's a bit more difficult:
var method = this.GetType().GetMethod("ValidateProperty");
foreach (var prop in unitProperties)
{
var parameter = Expression.Parameter(this.GetType(), "_");
var property = Expression.Property(parameter, prop);
var lambda = Expression.Lambda(property, parameter);
var genericMethod = method.MakeGenericMethod(prop.PropertyType);
genericMethod.Invoke(this, new object[] { lambda, prop.GetValue(this, null) });
}