2016-04-19 44 views
0

如何將方法調用轉換爲Action類,包括其參數?它甚至有可能嗎?被截獲Castle Windsor - 攔截方法並將其轉換爲Action類

Queue<Action> queue = new Queue<Action>(); 

樣品的方法:

public string DoSomeStuff(string[] arr) 
{ 
    //some logic here 
} 

攔截器(溫莎城堡):

public class MyInterceptor : IInterceptor 
{ 
    public void Intercept(IInvocation invocation) 
    { 
     if (queue.Count > 0) 
     { 
      queue.Enqueue(() => { 
       //here, add intercepted method (DoSomeStuff) and its parameters 
      }); 
     }     
    } 
} 

回答

1

入列是直截了當:

public void Intercept(IInvocation invocation) 
{ 
    queue.Enqueue(invocation.Proceed); 
    ... 
} 

但是,這是相當無意義因爲你會有等待委託出隊並調用,然後再從Intercept()方法返回。