我找到了解決方案。在這裏我創建了自定義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;
}
}