我找到了解决方案。在这里,我创建了自定义 NotEqual 属性。
public class RenameCompare
{
public string OldName { get; set; }
[NotEqual(PropName="OldName", ErrorMessage="The oldname and new name are equal!")]
public string NewName { get; set; }
}
public class NotEqualAttribute : ValidationAttribute
{
public string PropName { get; set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
PropertyInfo otherPropertyInfo = validationContext.ObjectType.GetProperty(PropName);
var otherPropertyStringValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null).ToString();
if (Equals(value.ToString(),otherPropertyStringValue))
{
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
return null;
}
}