如果属性在接口上定义,xVal 会起作用吗?

发布于 2024-08-02 03:08:23 字数 390 浏览 4 评论 0原文

我想知道是否有人知道如果我在模型类实现的接口上定义 system.componentmodel.dataannotations 属性,而不是直接在具体模型类上定义 xVal 是否会按预期工作。

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

然后在我的模型类中不会有任何验证属性...

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

如果我尝试使用 xVal 验证 FooFoo,它会使用其接口中的属性吗?

I'm wondering if anyone knows if xVal will work as expected if I define my system.componentmodel.dataannotations attributes on interfaces that are implemented by my model classes, instead of directly on the concrete model classes.

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

and then in my model class there wouldn't be any validation attributes...

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

If I try to validate a FooFoo with xVal, will it use the attribs from its interface?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心是晴朗的。 2024-08-09 03:08:23

目前,xVal.RuleProviders.DataAnnotationsRuleProvider 仅查看模型类本身定义的属性。 您可以在规则提供程序基类 PropertyAttributeRuleProviderBase 的方法 GetRulesFromProperty 中看到这一点:

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 参数表示模型类中的属性及其 < a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.memberdescriptor.attributes.aspx" rel="nofollow noreferrer">属性 属性仅表示直接在属性本身上定义的属性。

但是,您当然可以扩展 DataAnnotationsRuleProvider 并重写适当的方法以使其执行您想要的操作:从实现的接口中提取验证属性。 然后,您可以使用 xVal 注册您的规则提供程序:

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

要从已实现的接口中的属性获取属性,您应该扩展 DataAnnotationsRuleProvider 并重写 GetRulesFromTypeCore。 它获取类型为 System.Type 的参数 具有方法 GetInterfaces

At the moment the xVal.RuleProviders.DataAnnotationsRuleProvider only looks at properties defined on the model class itself. You can see this in the method GetRulesFromProperty in the rule provider base class 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;
}

The propertyDescriptor parameter represents a property in your model class and its Attributes property represents only the attributes defined directly on the property itself.

However, you could of course extend DataAnnotationsRuleProvider and override the appropriate method to make it do what you want: extract validation attributes from implemented interfaces. You then register your rule provider with xVal:

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

To get attributes from properties in implemented interfaces, you should extend DataAnnotationsRuleProvider and override GetRulesFromTypeCore. It gets a parameter of type System.Type that has a method GetInterfaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文