Unity IOC、AOP 和接口拦截
我一直在使用 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”
对这些人有任何帮助吗?
I've been playing around with Unity to do some AOP stuff, setting up via IOC like:
ioc.RegisterType<ICustomerService, CustomerService>()
.Configure<Interception>().SetInterceptorFor<ICustomerService>(new InterfaceInterceptor());
... and then having an ICallHandler on the ICustomerService interface's methods. For teh time being i want to just get the method called, the class it's in, and the namespace for that class. So... inside the...
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
...method of the ICallHandler, i can access the method name via input.MethodBase.Name... if i use input.MethodBase.DeclaringType.Name i get the interface ICustomerService... BUT... how would i go about getting the implementing class "CustomerService" rather than the interface?
I've been told to use input.Target.. but that just returns "DynamicModule.ns.Wrapped_ICustomerService_4f2242e5e00640ab84e4bc9e05ba0a13"
Any help on this folks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我前段时间尝试过做类似的事情,我通过 Unity 源代码做了一些研究,我想出的唯一解决方案是通过从
的私有字段反射来获取
(我不记得它的名字,你可以在调试器中轻松找到它)。我知道这不是一个优雅且可维护的解决方案,但看起来这就是我们所能做的。CustomerService
实例>input.TargetI've tried to do something similar some time ago, I've done some research through Unity source code and the only solution I came up with was to get
CustomerService
instance by reflection from private field ofinput.Target
(I don't remember its name, you can find it in debugger easily). I know this is not an elegant and maintanable solution, but it looks like this is all we can do.