簡體   English   中英

在Castle Windsor中,我可以注冊一個Interface組件並獲得實現的代理嗎?

[英]In Castle Windsor, can I register a Interface component and get a proxy of the implementation?

讓我們考慮一些情況:

_windsor.Register(Component.For<IProductServices>().ImplementedBy<ProductServices>().Interceptors(typeof(SomeInterceptorType));

在這種情況下,當我請求IProductServices時,windsor將代理該接口以攔截該接口方法調用。 如果相反,我這樣做:

_windsor.Register(Component.For<ProductServices>().Interceptors(typeof(SomeInterceptorType));

那么我不能要求Windsor解決IProductServices,而是要求ProductServices,它將返回一個動態子類,該子類將攔截虛擬方法調用。 當然,動態子類仍實現“ IProductServices”

我的問題是:是否可以像第一種情況一樣注冊Interface組件,並像第二種情況一樣獲得子類代理?

我想要這個有兩個原因:
1-因為要解析的代碼無法知道ProductServices類,所以只能知道IProductServices接口。 2-因為某些將發送者作為參數傳遞的事件調用將傳遞ProductServices對象,並且在第一種情況下,該對象是動態代理上的字段,而不是windsor返回的實際對象。 讓我舉個例子說明如何使事情復雜化:假設我有一個自定義集合,該集合在其項通知屬性更改時會執行某些操作:

private void ItemChanged(object sender, PropertyChangedEventArgs e)
    {
        int senderIndex = IndexOf(sender);
        SomeActionOnItemIndex(senderIndex);
    }

如果添加接口代理,此代碼將失敗,因為發送方將是接口代理中的字段,並且IndexOf(sender)將返回-1。

是的你可以:

_windsor.Register(Component.For<ProductServices, IProductServices>()
   .Interceptors(typeof(SomeInterceptorType));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM