2010-04-17 28 views
0

我有一個在類上實現INotifyPropertyChanged的方面。該方面包括如下內容:使用DataContractSerializer時的PostSharp?

[OnLocationSetValueAdvice, MethodPointcut("SelectProperties")] 
    public void OnPropertySet(LocationInterceptionArgs args) 
    { 
     var currentValue = args.GetCurrentValue(); 
     bool alreadyEqual = (currentValue == args.Value); 

     // Call the setter 
     args.ProceedSetValue(); 

     // Invoke method OnPropertyChanged (ours, the base one, or the overridden one). 
     if (!alreadyEqual) 
      OnPropertyChangedMethod.Invoke(args.Location.Name); 
    } 

當我實例化類正常,但我遇到問題時,我反序列化使用的DataContractSerializer類這工作得很好。這繞過了構造函數,我猜測它會干擾PostSharp設置自身的方式。這最終導致在攔截的屬性設置器中的NullReferenceException,但在它調用了自定義的OnPropertySet之前,所以我猜測它會干擾設置LocationInterceptionArgs。

有其他人遇到過這個問題嗎?有什麼方法可以解決它嗎?


我做了一些更多的研究,我發現我可以這樣解決問題:

[OnDeserializing] 
    private void OnDeserializing(StreamingContext context) 
    { 
     AspectUtilities.InitializeCurrentAspects(); 
    } 

我想,好吧,那不是太糟糕,所以我試圖做這在我的看點:

private IEnumerable<MethodInfo> SelectDeserializing(Type type) 
    { 
     return 
      type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(
       t => t.IsDefined(typeof (OnDeserializingAttribute), false)); 
    } 

    [OnMethodEntryAdvice, MethodPointcut("SelectDeserializing")] 
    public void OnMethodEntry(MethodExecutionArgs args) 
    { 
     AspectUtilities.InitializeCurrentAspects(); 
    } 

不幸的是,即使它正確截取了方法,它也不起作用。我在考慮對InitializeCurrentAspects的調用沒有得到正確的轉換,因爲它現在在Aspect中,而不是直接在aspect-enhanced類中。有沒有一種方法可以將此自動化,以便我不必擔心在每個想要擁有該方面的課程上調用此方法?

回答

相關問題