我正在尋找一個簡單的方法來執行正確執行INotifyPropertyChanged的即時的PropertyChanged提高它必須引用實際上是定義的屬性。我試着用Microsoft的新CodeContract工具來做這件事,但我不斷收到警告「CodeContracts:需要未經證實的」。這是我的代碼...執法的正確實施INotifyPropertyChanged的與CodeContracts - 「需要未經證實的」
public sealed class MyClass : INotifyPropertyChanged
{
private int myProperty;
public int MyProperty
{
get
{
return myProperty;
}
set
{
if (myProperty == value)
{
return;
}
myProperty = value;
OnPropertyChanged("MyProperty");
}
}
private void OnPropertyChanged(string propertyName)
{
Contract.Requires(GetType().GetProperties().Any(x => x.Name == propertyName));
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
有沒有辦法讓這個工作?
靜態檢查器不會反射來驗證合同。 http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/37e28a50-bf64-4b02-b384-f55117735690/ – hwiechers 2009-12-03 04:12:34