Say I have an interface IInterface. Say I have 2 imple开发者_开发百科mentations of the same IInterface (foo & bar). Is it possible to invoke the same method on both targets?
It depends how you approach it,
Generally its not possible (which return value should be returned?), but nothing stops you from having another target wrapped in an interceptor, and having it invoked by the interceptor.
I came up with this, but it uses reflection so it's not as good as "native" support for Y-adapter type of proxy...
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
ThreadPool.QueueUserWorkItem(new WaitCallback(
(object o) =>
{
invocation.Method.Invoke(newTarget, invocation.Arguments);
}
)
);
}
Using the QueueUserWorkItem guarantees that the thread invoking the method is not going to suffer much in terms of performance... Any better solution is more than welcome!
精彩评论