Skip to content
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.

Intercepted method always returns null or equivalent #8

Open
rainynovember12 opened this issue Nov 1, 2010 · 0 comments
Open

Intercepted method always returns null or equivalent #8

rainynovember12 opened this issue Nov 1, 2010 · 0 comments

Comments

@rainynovember12
Copy link

The intercepted method in the sample interception program always returns null (Or, equivalent), no matter what actual value the method returns.

Let's change the Pay() method of Employee class to return an int

public int Pay(int wage)
{

var currentAmount = wage;
//_checkingAccount.Deposit(currentAmount);

Console.WriteLine("Paid {0} units", wage);
return wage;

}

The, Intercept method is already there which invokes the target method and and tries to get the return value from within the intercepted method:

public object Intercept(IInvocationInfo info)
{

var methodName = info.TargetMethod.Name;
Console.WriteLine("method '{0}' called", methodName);

// Replace the input parameter with 42
var result = info.TargetMethod.Invoke(info.Target, new object[]{42});
//Expecting to get the return value correctly from the target method. 
return result;
//The return value is always null or equivalent (0 here)

}

Unfortunately, the return value from the target method invocation (info.TargetMethod.Invoke()) is always null or equivalent (0 here). So, when I try to postweave some of my existing dlls in a project, the project fails to run (Cause, the methods calls no longer return any value after post-weaving).

Using a reflector, I took a look at the post weaven Pay() method, after modifying the Pay() as returning an int. There I found the following codes, along with the injected codes

... int amount = wage; this._checkingAccount.Deposit(amount);
Console.WriteLine("Paid {0} units", wage);
int num2 = wage;
....

return (int) obj2;

//I've found that, the obj2 is never assigned with a value in the post weaven codes and hence, the postweaven methods always return null. This may give you some clue.

Please note that, the First version of LinFu.AOP was able to return the correct value from the postweaven intercepted methods (It had other issues though). So, I hope this issue should also be an easy fix.

Thanks
Comments

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant