使用反射的自定义验证属性?
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用属性检查其他属性并不是那么容易。 这里的其他人也问了同样的问题(如本链接所示),答案不是很漂亮。 此处的另一个答案建议使用 IDataErrorInfo 执行完整的类验证。
Examining other properties with an attribute isn't all that easy. Other people on here have asked the same question (as seen in this link) and the answer's, well, not super pretty. Another answer on here recommends using IDataErrorInfo to perform full class validation.