当您需要比较对象的多个属性作为验证的一部分时,您需要一个claass验证器。该属性然后应用于类,而不是属性。
我不认为有现成的人可以做你想做的事,但很容易写出你自己的想法。
这里是一个代码大纲向您展示您的验证可能看起来大概是什么样
[AttributeUsage(AttributeTargets.Class)]
[ValidatorClass(typeof(ReferencedByValidator))]
public class GreaterThanOrEqualAttribute : EmbeddedRuleArgsAttribute, IRuleArgs
{
public GreaterThanOrEqualAttribute(string firstProperty, string secondProperty)
{
/// Set Properties etc
}
}
public class ReferencedByValidator : IInitializableValidator<GreaterThanOrEqualAttribute>
{
#region IInitializableValidator<ReferencedByAttribute> Members
public void Initialize(GreaterThanOrEqualAttribute parameters)
{
this.firstProperty = parameters.FirstProperty;
this.secondProperty = parameters.SecondProperty;
}
#endregion
#region IValidator Members
public bool IsValid(object value, IConstraintValidatorContext constraintContext)
{
// value is the object you are trying to validate
// some reflection code goes here to compare the two specified properties
}
#endregion
}
}
我认为通过流畅的样式映射可能会更容易完成此操作。你应该看看这些文章[这里](http://fabiomaulo.blogspot.com/search/label/Validator)。 – 2011-05-17 11:54:35