Will xVal work if attributes are defined on interfaces?

I am wondering if anyone knows if xVal will work properly if I define the system.componentmodel.dataannotations attributes on interfaces that are implemented by my model classes, and not directly on specific model classes.

public interface IFoo
{
    [Required] [StringLength(30)]
    string Name { get; set; }
}

and then in my model class there would be no validation attributes ...

public class FooFoo : IFoo
{
    public string Name { get; set; }
}

If I try to check FooFoo on xVal, will they use attributes from their interface?

+3
source share
1 answer

xVal.RuleProviders.DataAnnotationsRuleProvider , . GetRulesFromProperty PropertyAttributeRuleProviderBase:

protected virtual IEnumerable<Rule> GetRulesFromProperty(
    PropertyDescriptor propertyDescriptor)
{
    return from att in propertyDescriptor.Attributes.OfType<TAttribute>()
           from validationRule in MakeValidationRulesFromAttribute(att)
           where validationRule != null
           select validationRule;
}

propertyDescriptor , Attributes , .

, , DataAnnotationsRuleProvider , , : . xVal:

ActiveRuleProviders.Providers.Clear();
ActiveRuleProviders.Providers.Add(new MyDataAnnotationsRuleProvider());
ActiveRuleProviders.Providers.Add(new CustomRulesProvider());

, DataAnnotationsRuleProvider GetRulesFromTypeCore. System.Type, GetInterfaces.

+4

Source: https://habr.com/ru/post/1715193/


All Articles