请在下面查看我的示例:
模型类实现INotifyPropertyChanged
public class ModelClass : INotifyPropertyChanged
{
private string destinationCity;
public string SourceCity { get; set; }
public ModelClass()
{
PropertyChanged += CustomAttribute.ThrowIfNotEquals;
}
[Custom("SourceCity", ErrorMessage = "the source and destination should not be equal")]
public string DestinationCity
{
get
{
return this.destinationCity;
}
set
{
if (value != this.destinationCity)
{
this.destinationCity = value;
NotifyPropertyChanged("DestinationCity");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
属性类还包含事件处理程序。
internal sealed class CustomAttribute : Attribute
{
public CustomAttribute(string propertyName)
{
PropertyName = propertyName;
}
public string PropertyName { get; set; }
public string ErrorMessage { get; set; }
public static void ThrowIfNotEquals(object obj, PropertyChangedEventArgs eventArgs)
{
Type type = obj.GetType();
var changedProperty = type.GetProperty(eventArgs.PropertyName);
var attribute = (CustomAttribute)changedProperty
.GetCustomAttributes(typeof(CustomAttribute), false)
.FirstOrDefault();
var valueToCompare = type.GetProperty(attribute.PropertyName).GetValue(obj, null);
if (!valueToCompare.Equals(changedProperty.GetValue(obj, null)))
throw new Exception("the source and destination should not be equal");
}
}
用法
var test = new ModelClass();
test.SourceCity = "1";
// Everything is ok
test.DestinationCity = "1";
// throws exception
test.DestinationCity ="2";
为了简化代码,我决定省略验证。