As far as I know, there is no way to interact with reference types in expression trees. (for example, nothing generates the operation code stind.* or ldind.* ). I am working on a copyist to get around this annoyance. Since I'm creating a new type in which the body of the method is replaced with delegated delegates (to get around the fact that CompileToMethod can only execute static methods that cannot interact with new members). For the by-ref and out parameters, I thought I replaced them using StrongBox<T> .
So, if I come across a method that has a signature that looks like this:
public class SomeClass { public virtual bool SomeMethod(string arg1,ref int arg2) { } }
The override, the callbase method, and the delegate field that I create will look like this:
public class SomeClass<1> : SomeClass { private static bool SomeMethod<0>( SomeClass target,string arg1,StrongBox<int> arg2) { return call target.SomeMethod(arg1,ref arg2.Value) } private Func<SomeClass,string,StrongBox<int>,bool> <0>SomeMethod; public override bool SomeMethod(string arg1,ref int arg2) { StrongBox<int> box = new StrongBox<int>(); box.Value = arg2; bool retVal = <0>SomeMethod.Invoke(this,arg1,box); arg2 = box.Value; return retVal; } }
However, there is a lot of code for this conversion, for each parameter it introduces a lot of complexity. It would be much simpler if I configured box.Value = arg2 , if I could do something like &box.Value = &arg2 , which assigns it the address arg2 in its current form. Thus, when a delegate mutates in a value field, the changes are forwarded. This means that I do not need to have a variable to store the return value, and I do not need to update the reference value.
Alternatively, if there is a way to do the ref-assignment semantics with expression trees, I, of course, are all ears.