Hello,
I'm using MethodCallExpression to record method calls.
public void RegisterInvocation<TSource>(TSource target, Expression<Action<TSource>> selector)
{
...
}
Somewhen later I execute the expression like this:
selector.Compile().Invoke();
And here I have a strange effekt (perhaps I missunderstand something with Method Call Expressions).
If I register a method call with normal variable or constant arguments, the methods gets called with the correct arguments:
string item = "sometext";
instance.RegisterInvocation<ITarget>(this, p => p.Add(item));
But if I register a method call with an instance variable arguments, the method gets called with argument values of the instance variable at execution time and not at registration time:
public class Target : ITarget
{
string item;
public void DoSomething()
{
this.item = "sometext";
instance.RegisterInvocation<ITarget>(this, p => p.Add(this.item));
this.item = "anothertext";
instance.CallRegisteredInvocation();
}
public void Add(string item)
{
// "somestring" expected, but item is = "anotherstring"
}
}
Is there a way to invoke the method call expression with arguments at registration time?
Thanks for help, Enyra