【问题标题】:Unity IOC, AOP & Interface InterceptionUnity IOC、AOP 和接口拦截
【发布时间】:2010-10-14 18:58:58
【问题描述】:

我一直在使用 Unity 做一些 AOP 的东西,通过 IOC 进行设置,例如:

ioc.RegisterType<ICustomerService, CustomerService>()
    .Configure<Interception>().SetInterceptorFor<ICustomerService>(new InterfaceInterceptor());

... 然后在 ICustomerService 接口的方法上有一个 ICallHandler。目前我只想获取调用的方法、它所在的类以及该类的命名空间。所以...里面...

public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) 

...ICallHandler 的方法,我可以通过 input.MethodBase.Name 访问方法名称...如果我使用 input.MethodBase.DeclaringType.Name 我得到接口 ICustomerService...但是...如何我去获取实现类“CustomerService”而不是接口?

有人告诉我使用 input.Target.. 但它只会返回“DynamicModule.ns.Wrapped_ICustomerService_4f2242e5e00640ab84e4bc9e05ba0a13”

对这些人有帮助吗?

【问题讨论】:

    标签: c# .net inversion-of-control unity-container aop


    【解决方案1】:

    前段时间我尝试做类似的事情,我通过 Unity 源代码进行了一些研究,我想出的唯一解决方案是通过 input.Target 的私有字段的反射获取 CustomerService 实例(我不记得它的名字了,你可以很容易地在调试器中找到它)。我知道这不是一个优雅且可维护的解决方案,但看起来这就是我们所能做的。

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 2015-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多