2015-11-26 53 views
0

我創建了一個簡單的方面:適用於其他裝配類方法的方面都需要

[Serializable()] 
public class NullableCallAspect : PostSharp.Aspects.OnMethodBoundaryAspect 
{ 
    public override void OnEntry(PostSharp.Aspects.MethodExecutionArgs args) 
    { 
     if (args.Instance == null) 
      args.FlowBehavior = PostSharp.Aspects.FlowBehavior.Return; 
    } 
} 

從本質上講,我想無論哪instance.method呼叫instance == null它不輸入方法。我搞清楚了,我需要改變方面的繼承。所以,我需要換一個OnMethodBoundaryAspect。這將是第一個問題。

另一個問題是如何將這個方面應用到繼承另一個程序集接口的類的方法調用。

我已經試過這一點,但它不是做得比較工作:

[assembly: UI.Aspects.NullableCallAspect(
    AttributeTargetAssemblies = "UIAppearanceExtensibility", 
    AttributeTargetTypes = "UI.Appearance.Extensibility.*.I*AppearanceManager", 
    AttributeTargetMembers = "handle*" 
)] 

回答

1

這種方面將要求其不受PostSharp支持通話網站攔截。 OnMethodBoundaryAspect和MethodInterceptionAspect都修改目標方法不會調用網站本身 - 在調用由這些方面裝飾的方法時仍然需要實例。

編輯: 有一個黑客如何強制PostSharp攔截呼叫站點。當一個方面被組裝成不同類型的裝配時。如果所有方法都在項目實施ClassLibrary1的,他們是從爲MyApplication項目,然後方面可以MyApplication的項目進行多播這樣就叫:

[assembly: 
    NullableCallAspect(AttributeTargetAssemblies = "ClassLibrary1", AttributeTargetTypes = "ClassLibrary1.*", 
     AttributeTargetMembers = "handle*", AttributeTargetElements = MulticastTargets.Method)] 

如果有一個慣例,所有的實施IAppearanceManager端具有後綴名AppearanceManager然後組播需要改變:

[assembly: 
    NullableCallAspect(AttributeTargetAssemblies = "ClassLibrary1", AttributeTargetTypes = "ClassLibrary1.*AppearanceManager", 
     AttributeTargetMembers = "handle*", AttributeTargetElements = MulticastTargets.Method)] 

如果沒有這樣的約定,那麼IAspectProvider可以用於多播。

當在相同程序集內調用由NullableCallAspect修飾的方法時,此方法不可用 - 在此情況下,呼叫站點不會被攔截。

+0

是的,每個接口都在** ClassLibrary1 **上定義,每個對其方法的調用都是由** MyApplication1 **完成的。正如你可以看到[這裏](http://s16.photobucket.com/user/jeusdi/media/snip3.png.html),** MyApplication1 **是'UI'和** ClassLibrary1 **是'UIExtensibility' 。但它不起作用。我不知道如何解決這個問題。 – Jordi

+0

使ClassLibrary1中的所有接口都無法使黑客工作是不夠的。所有的接口實現都需要在ClassLibrary1中定義。 –

+0

好的。我想避免檢查'null.method'調用。我爲我的類型創建了一個虛擬實現,其中IoC不提供任何實現。虛擬實現由NSubstitute代理提供。感謝所有。 – Jordi