我想了解如何在Unity中使用调用处理程序。下面的代码我到目前为止:为什么我的自定义调用处理程序未被调用?
void Main()
{
var container = new UnityContainer();
container.AddNewExtension<Interception>()
.Configure<Interception>()
.AddPolicy("TestPolicy")
.AddCallHandler(new TestCallHandler());
container.RegisterType<IFoo, Foo>();
var foo = container.Resolve<IFoo>();
foo.Test();
}
interface IFoo
{
void Test();
}
class Foo : IFoo
{
public void Test()
{
"Foo.Test()".Dump();
}
}
class TestCallHandler : ICallHandler
{
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
Console.WriteLine("[Interceptor] Calling {0}.{1}", input.MethodBase.DeclaringType.FullName, input.MethodBase.Name);
return getNext()(input, getNext);
}
public int Order { get; set; }
}
但TestCallHandler.Invoke
不会被调用,它只是直接调用Foo.Test
。我错过了什么?
谢谢!它确实解决了这个问题。我认为可以在全球范围内为所有类型进行。 –
MSDN文档警告说TransparentProxyInterceptor速度很慢。建议使用InterfaceInterceptor。 https://msdn.microsoft.com/en-us/library/dn178466(v=pandp.30).aspx#sec9 – Calvin