2014-12-04 16 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

相关问题