我有一組方法允許用戶輕鬆使用PropertHasChanged事件並允許然後執行一些額外的處理。這裏是方法:以操作爲參數的覆蓋方法
public virtual void SetPropertyValue<T>(ref T currentValue, T newValue, Action<T> extraFunction = null, Action voidAfterSetAction = null) where T : class
{
if (currentValue == newValue) return;
currentValue = newValue;
PropertyHasChanged();
if (extraFunction != null) extraFunction(newValue);
if (voidAfterSetAction != null) voidAfterSetAction();
}
對我來說,我已經明白,我有時需要extraFunction行動中的舊值。這是我打算這麼做的:
public virtual void SetPropertyValue<T>(ref T currentValue, T newValue, Action<T, T> extraFunction = null, Action voidAfterSetAction = null) where T : class
{
var oldVal = currentValue;
if (currentValue == newValue) return;
currentValue = newValue;
PropertyHasChanged();
if (extraFunction != null) extraFunction(oldVal, newValue);
if (voidAfterSetAction != null) voidAfterSetAction();
}
正如您可能注意到的那樣,extraFunction操作現在需要兩個參數。 VS沒有與我創建方法的問題(沒有紅色的qwigglies),但是當我建立它會引發很多錯誤,說第一種方法和第二種方法之間的用法是模棱兩可的。如果是這樣的話,我該如何實現我所期望的?
EDIT
下面是該方法的通常的用法:
SetPropertyValue(ref _streetAddress1, value, null,() => SalesData.StreetAddress1 = value);
正如Jon所要求的:請向我們展示實際的呼叫站點,因爲這是導致問題的原因。你有「很多」的錯誤,因爲你調用的方法「很多」次! – dlev 2012-02-29 18:50:39
我更新了。我認爲喬恩的建議是合理的(當然)。我只是想改變它的名字。 – 2012-02-29 18:59:47
@TheSheekGeek在我的(可能是不正確的)觀點中,除了內置的「基元」類型之外,對任何其他東西使用可選參數幾乎不是一個好主意。 – asawyer 2012-02-29 19:02:00