2010-05-07 24 views
1

我想讓StructureMap將Castle.DynamicProxy放置在它創建的一些對象周圍。我之前使用過EnrichWith功能,但我認爲RegisterInterception在這種情況下更適合我,因爲我使用掃描。帶有攔截和Castle.DynamicProxy的StructureMap

問題是,在「進程(對象目標,IContext上下文)」 - 方法中,我找不到哪個接口SM試圖獲取,只有具體的類。我可以找到這個類實現的所有接口,但是如果它實現了多個接口,我不知道如何找到實際請求的接口。有沒有辦法做到這一點?

下面是一些代碼:

public class SMInterceptor : TypeInterceptor 
    { 
     private readonly IInterceptor _interceptor; 
     private readonly ProxyGenerator _proxyGenerator; 


     public SMInterceptor(IInterceptor interceptor, ProxyGenerator proxyGenerator) 
     { 
      _interceptor = interceptor; 
      _proxyGenerator = proxyGenerator; 
     } 

     public static List<Type> TypesToIntercept = new List<Type>(); 

     public object Process(object target, IContext context) 
     { 
      var interfaceToTarget = // This is where I want the target interface! 
      var decorator = _proxyGenerator.CreateInterfaceProxyWithTarget(interfaceToTarget, target, _interceptor); 
      return decorator; 
     } 

     public bool MatchesType(Type type) 
     { 
      return true; 
     } 
    } 

回答

0

有點晚了,但下面的代碼應該工作(假定請求類型是一個實例)

var interfaceToTarget = context.BuildStack.Current.RequestedType; 
+0

這不工作很正確。我已經在這裏一個簡單的控制檯應用程序例如:http://pastie.org/1088333 這將返回: 接口:ConsoleApplication1.IOne 目標:ConsoleApplication1.Two 而我真正想要的是 這不起作用完全正確。我在這裏做了一個簡單的控制檯應用程序示例:http://pastie.org/1088333 此返回: interface:ConsoleApplication1.ITwo – gautema 2010-08-12 14:21:24