简体   繁体   English

如何在 FluentValidation 中使用反射?

[英]How to use Reflection in FluentValidation?

I have a scneario in which I want to use reflection to do validation using FluentValidation.我有一个场景,我想在其中使用反射来使用 FluentValidation 进行验证。 Someting like this:像这样:

public class FooValidator : AbstractValidator<Foo>
{
    public FooValidator(Foo obj)
    {
        // Iterate properties using reflection
        var properties = ReflectionHelper.GetShallowPropertiesInfo(obj);
        foreach (var prop in properties)
        {
            // Create rule for each property, based on some data coming from other service...
            //RuleFor(o => o.Description).NotEmpty().When(o => // this works fine when foo.Description is null
            RuleFor(o => o.GetType().GetProperty(prop.Name)).NotEmpty().When(o =>
            {
                return true; // do other stuff...
            });
        }
    }
}

The call to ReflectionHelper.GetShallowPropertiesInfo(obj) returns "shallow" properties of object. Then, for each property I create a rule.ReflectionHelper.GetShallowPropertiesInfo(obj)的调用返回 object 的“浅”属性。然后,我为每个属性创建一个规则。 This is my code for get properties of object:这是我获取 object 属性的代码:

public static class ReflectionHelper
{
    public static IEnumerable<PropertyInfo> GetShallowPropertiesInfo<T>(T o) where T : class
    {

        var type = typeof(T);
        var properties =
            from pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
            where pi.PropertyType.Module.ScopeName == "CommonLanguageRuntimeLibrary"
                && !(pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>))
            select pi;

        return properties;
    }
}

This code compile and can be executed此代码编译并可以执行

IValidator<Foo> validator = new FooValidator(foo);
var results = validator.Validate(foo);

But it doesn't work properly (validation never fails).但它不能正常工作(验证永远不会失败)。 Is there any way to achieve this?有什么办法可以做到这一点?

Note: Demo code can be found int Github at FluentValidationReflection注意:演示代码可以在 FluentValidationReflection 找到 int Github

Finally I've found a solution that works. 最后,我找到了一个可行的解决方案。 I've created a custom PropertyValidator that receives a PropertyInfo parameter: 我已经创建了一个接收PropertyInfo参数的自定义PropertyValidator:

public class CustomNotEmpty<T> : PropertyValidator
{
    private PropertyInfo _propertyInfo;

    public CustomNotEmpty(PropertyInfo propertyInfo)
        : base(string.Format("{0} is required", propertyInfo.Name))
    {
        _propertyInfo = propertyInfo;
    }

    protected override bool IsValid(PropertyValidatorContext context)
    {
        return !IsNullOrEmpty(_propertyInfo, (T)context.Instance);
    }

    private bool IsNullOrEmpty(PropertyInfo property, T obj)
    {
        var t = property.PropertyType;
        var v = property.GetValue(obj);

        // Omitted for clarity...
    }
}

And a extension method for IRuleBuilder : 还有IRuleBuilder的扩展方法:

public static class ValidatorExtensions
{
    public static IRuleBuilderOptions<T, T> CustomNotEmpty<T>(
        this IRuleBuilder<T, T> ruleBuilder, PropertyInfo propertyInfo)
    {
        return ruleBuilder.SetValidator(new CustomNotEmpty<T>(propertyInfo));
    }
}

With this I can change my FooValidator as follows: 这样,我可以如下更改FooValidator

public class FooValidator : AbstractValidator<Foo>
{
    public FooValidator(Foo obj)
    {
        // Iterate properties using reflection
        var properties = ReflectionHelper.GetShallowPropertiesInfo(obj);
        foreach (var prop in properties)
        {
            // Create rule for each property, based on some data coming from other service...
            RuleFor(o => o)
                .CustomNotEmpty(obj.GetType().GetProperty(prop.Name))
                .When(o =>
            {
                return true; // do other stuff...
            });
        }
    }
}

And now I'm be able to use Reflection to add rules for specific properties 现在,我可以使用Reflection添加特定属性的规则

For reflection i use为了反思我使用

RuleFor(x => new obj().GetType().GetProperty(x.Filter.ColumnName)).NotNull(); RuleFor(x => new obj().GetType().GetProperty(x.Filter.ColumnName)).NotNull();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM