public void Validate(object model, ValidatorBase validator) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (validator == null) { throw new ArgumentNullException(nameof(validator)); } if (!(model is T typedModel)) { throw new ArgumentException($"The passed model is not of type {typeof(T).FullName} but of type {model.GetType().FullName}", nameof(model)); } if (!typeof(T).IsAssignableFrom(typeof(TEntity))) { throw new Exception($"Cannot convert {typeof(TEntity).FullName} to {typeof(T).FullName}. Use the overload of the {nameof(Validate)} method that defines a conversion expression."); } var parameter = Expression.Parameter(typeof(TEntity), "x"); var lambda = typeof(T) == typeof(TEntity) ? Expression.Lambda <Func <TEntity, T> >(parameter, parameter) : Expression.Lambda <Func <TEntity, T> >(Expression.Convert(parameter, typeof(T)), parameter); validateAction.Invoke(typedModel, validator, lambda); }
public void Validate(T model, ValidatorBase validator, Expression <Func <TEntity, T> > entityExpression) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (validator == null) { throw new ArgumentNullException(nameof(validator)); } validateAction.Invoke(model, validator, entityExpression); }
public void Validate(object model, ValidatorBase validator, LambdaExpression entityExpression) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (validator == null) { throw new ArgumentNullException(nameof(validator)); } if (!(model is T typedModel)) { throw new ArgumentException($"The passed model is not of type {typeof(T).FullName} but of type {model.GetType().FullName}", nameof(model)); } validateAction.Invoke(typedModel, validator, Expression.Lambda <Func <TEntity, T> >(entityExpression.Body, entityExpression.Parameters)); }