开发者

Castly Dynamic Proxy - Get the target method's return value

开发者 https://www.devze.com 2023-03-22 19:51 出处:网络
When proxying an object and intercepting a method using castle dynamic proxy, is it possible to get the return value of the target method? I have tried using the following methods,

When proxying an object and intercepting a method using castle dynamic proxy, is it possible to get the return value of the target method? I have tried using the following methods,

object result = invocation.GetConcreteMethod().Invoke(instance, null); 

object result = invocation.GetConcreteMethodInvocationTarget().Invoke(instance, null); 

This causes an infinite loop. I want to be able to get the return value of the original target method being proxied before calling Invocation.Proceed().

EDIT -- Fyi I got it to work by using Activator.CreateInstanc开发者_StackOverflow社区e, but I'm wondering if there is a cleaner way to achieve the equivalent:

object instance = Activator.CreateInstance(invocation.TargetType); 

invocation.MethodInvocationTarget.Invoke(instance, invocation.Arguments); 

The problem is that this is simply a new un-proxied instance of the original object, whereas I want the original un-proxied instance itself.


invocation.Proceed();

var returnValue = invocation.ReturnValue;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号