4

There is as far as I know no way to interact with reference types in Expression trees. (e.g. nothing emits a stind.* or a ldind.* opcode). I'm working on a bit of a rewriter to get around this annoyance. Since I'm building a new type that has the method body replaced with delegate invocations (to get around the fact that CompileToMethod can only do static methods that can't interact with new members). For by-ref and out parameters, I thought I'd replace their usages with StrongBox<T>.

So if I came 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 I generate 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, this is quite a lot of code to perform this conversion, for each parameter it introduces a lot of complexity. It would be much easier when I perform the setting of box.Value = arg2, if I could do something like &box.Value = &arg2 that is assign it's address to the address of arg2 as it stands. That way when the delegate performs a mutation on the value field the changes are forwarded. Doing this means I don't need to have to have a variable to hold the return value, and I don't need to perform a reference value update.

Alternatively, if there is a way to perform assign-by-ref semantics with Expression trees, I'm all ears of course.

Michael B
  • 7,512
  • 3
  • 31
  • 57
  • This makes me think of Eric Lippert's [Ref Class](http://stackoverflow.com/questions/2980463/how-do-i-assign-by-reference-to-a-class-field-in-c/2982037#2982037). – Brian Dec 06 '10 at 17:14

1 Answers1

0

Not sure if I really understand but maybe this is a solution:

class Program
{

    public class SomeClass
    {
        private readonly int _n;

        public SomeClass(int n) { _n = n; }

        public virtual bool SomeMethod(string arg1, ref int arg2) {
            if (String.IsNullOrWhiteSpace(arg1)) return false;
            arg2 += arg1.Length + _n;
            return true;
        }
    }

    private delegate bool SomeDelegate(SomeClass that, string arg1, ref int arg2);

    static void Main(string[] args) {
        var instance = Expression.Parameter(typeof (SomeClass), "that");
        var arg1Param = Expression.Parameter(typeof(string), "arg1");
        var arg2Param = Expression.Parameter(typeof (int).MakeByRefType(), "arg2");
        var someMethodInfo = typeof (SomeClass).GetMethod("SomeMethod");
        var lambda = Expression.Lambda<SomeDelegate>(Expression.Call(instance, someMethodInfo, arg1Param, arg2Param), instance, arg1Param, arg2Param);
        var someDelegate =lambda.Compile();
        var myClass = new SomeClass(2);
        var arg1 = "yup";
        var arg2 = 1;
        var result = someDelegate(myClass, arg1, ref arg2);
        if(arg2 != 6) throw new Exception("Bad!");
        Console.WriteLine("works...");
    }

}

The important bit I think is typeof (int).MakeByRefType() .

Dandy
  • 272
  • 3
  • 11