2

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.CreateInstance, 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.

Sean Thoman
  • 7,429
  • 6
  • 56
  • 103

1 Answers1

8
invocation.Proceed();

var returnValue = invocation.ReturnValue;
Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115