Search code examples
c#reflectioncastle-windsorcastle-dynamicproxy

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


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.


Solution

  • invocation.Proceed();
    
    var returnValue = invocation.ReturnValue;