2014-12-04 38 views
0

我用下面的代碼在某些方面實現INotifyPropertyChangedLocationInterceptionAspect.OnSetValue(LocationInterceptionArgs args):如何檢查屬性是否爲索引器並獲取其值?

[Serializable] 
public class NotifyPropertyChangedAspect : LocationInterceptionAspect { 
    /* ... stuff ... */ 
    public override void OnSetValue(LocationInterceptionArgs args) 
    { 
     var obj = args.Instance; 
     /* ... more stuff ... */ 
     var oldValue = args.Binding.GetValue(ref obj, Arguments.Empty); 
     /* ... extra stuff ... */ 
    } 
} 

但如果我的屬性是一個索引,那麼它失敗的異常InvalidCastException

Unable to cast object of type 'PostSharp.Aspects.Internals.Arguments`1[PostSharp.Aspects.Arguments]' to type 'PostSharp.Aspects.Internals.Arguments`1[System.Int32]'.

如何檢查如果該屬性是一個索引器,並獲得它的值(我寧願在編譯過程中進行檢查,我猜在CompileTimeInitialize方法中)?

回答

0

您可以將LocationInterceptionArgs.Index屬性的值傳遞給Binding.GetValue方法。該屬性已經包含正確的參數值。

var oldValue = args.Binding.GetValue(ref obj, args.Index) 

另一種選擇是撥打args.GetCurrentValue()來代替。

-alex

相關問題