使用反射的自定义验证属性?

发布于 2025-01-08 15:42:41 字数 1252 浏览 1 评论 0原文

我想使用 System.ComponentModel.DataAnnotations 程序集来验证我正在处理的控制台应用程序的参数(映射到属性)。我将使用“伙伴类”元数据模式;过去它对我来说效果很好。

我需要验证的一件事是提供了两种类型的参数之一。换句话说,可以指定参数 foo 或参数 bar,但不能同时指定两者,也不能两者都指定。

为此,我开始编写一个自定义验证属性,这看起来相当简单,但是当我意识到我需要到达验证上下文的属性之外,并遍历到我正在验证的对象中的同级属性时,我有点迷失了(就像CompareAttribute)。这似乎是一个经典的反思案例,但我不知道如何继续。这就是我到目前为止所得到的:

/// <summary>
/// This property, or the other specified, are required, but both cannot be set.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class XORAttribute : ValidationAttribute
{
    /// <summary>
    /// If validation should fail, return this error message.
    /// </summary>
    public string ErrorMessage { get; set; }
    /// <summary>
    /// The name of the other required property that is mutually exclusive of this one.
    /// </summary>
    public string OtherValueName { get; set; }

    public XORAttribute(string otherValueName)
    {
        this.OtherValueName = otherValueName;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        //Something needs to go here.
    }
}

这里的一些帮助将不胜感激。

I want to use the System.ComponentModel.DataAnnotations assembly to validate arguments (mapped to properties) for a console app I'm working on. I'll be using the "buddy class" metadata pattern; it's worked well for me in the past.

One of the things I need to validate is that exactly one of two types of arguments are provided. In other words, argument foo can be specified, or argument bar, but not both, and not neither.

To this end I started writing a custom validation attribute, which seemed fairly straightforward, but I got a bit lost when I realized I needed to reach outside the property of the validation context, and traverse to a sibling property in the object I am validating (like the CompareAttribute). It seems this is a classic reflection case, but I am scratching my head as to how to proceed. This is what I have so far:

/// <summary>
/// This property, or the other specified, are required, but both cannot be set.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class XORAttribute : ValidationAttribute
{
    /// <summary>
    /// If validation should fail, return this error message.
    /// </summary>
    public string ErrorMessage { get; set; }
    /// <summary>
    /// The name of the other required property that is mutually exclusive of this one.
    /// </summary>
    public string OtherValueName { get; set; }

    public XORAttribute(string otherValueName)
    {
        this.OtherValueName = otherValueName;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        //Something needs to go here.
    }
}

Some assistance here would be appreciated.

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

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

发布评论

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

评论(1

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