Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In addition to Jim's point about MakeByRefType, you will need to create a custom delegate type, since <code>Func</code> does not support ref or out parameters. Putting it all together: </p> <pre><code>delegate bool ValidateDelegate(Program program, out bool validatedControlAllowsFocusChange); static void Main(string[] args) { var type = typeof(Program); var methodInfo = type.GetMethod("ValidateActiveControl", BindingFlags.Instance | BindingFlags.NonPublic); var p1 = Expression.Parameter(type, "program"); var p2 = Expression.Parameter(typeof(bool).MakeByRefType(), "validatedControlAllowsFocusChange"); var invokeExpression = Expression.Call(p1, methodInfo, p2); var func = Expression.Lambda&lt;ValidateDelegate&gt;(invokeExpression, p1, p2).Compile(); var validatedControlAllowsFocusChange = true; // I would expect validatedControlAllowsFocusChange to be false after execution... Console.WriteLine(func.Invoke(new Program(), out validatedControlAllowsFocusChange)); Console.WriteLine(validatedControlAllowsFocusChange); } </code></pre> <hr> <p>Edit: This works in .NET 4.0, but not in .NET 3.5. The 3.5 Framework doesn't appear to support lambda expression trees with out or ref parameters. This code: </p> <pre><code>delegate void RefTest(out bool test); static void Main(string[] args) { var p1 = Expression.Parameter(typeof(bool).MakeByRefType(), "test"); var func = Expression.Lambda&lt;RefTest&gt;(Expression.Constant(null), p1); } </code></pre> <p>throws an ArgumentException "A lambda expression cannot contain pass by reference parameters". I don't think you can do what you want without upgrading to .NET 4.0. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload