Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to create an assignment expression, simply create an expression that invokes a generic assignment routine. The class below provides a nice extension method that will convert any field access expression into a field assignment expression:</p> <pre><code>using System; using System.Linq.Expressions; using System.Reflection; namespace Ethica.Expressions { public static class ExpressionExtenstions { private class AssignmentHelper&lt;T&gt; { private static void SetValue(ref T target, T value) { target = value; } internal static MethodInfo MethodInfoSetValue = typeof(AssignmentHelper&lt;T&gt;).GetMethod("SetValue", BindingFlags.NonPublic | BindingFlags.Static); } public static Expression&lt;Action&lt;TInstance, TProp&gt;&gt; ToFieldAssignExpression&lt;TInstance, TProp&gt; ( this Expression&lt;Func&lt;TInstance, TProp&gt;&gt; fieldGetter ) { if (fieldGetter == null) throw new ArgumentNullException("fieldGetter"); if(fieldGetter.Parameters.Count != 1 || !(fieldGetter.Body is MemberExpression)) throw new ArgumentException ("Input expression must be a single parameter field getter, e.g. g =&gt; g._fieldToSet or function(g) g._fieldToSet"); ParameterExpression[] parms = new ParameterExpression[] { fieldGetter.Parameters[0], Expression.Parameter(typeof(TProp), "value")}; Expression body = Expression.Call(AssignmentHelper&lt;TProp&gt;.MethodInfoSetValue, new Expression[] { fieldGetter.Body, parms[1] }); return Expression.Lambda&lt;Action&lt;TInstance, TProp&gt;&gt;(body, parms); } public static Action&lt;TInstance, TProp&gt; ToFieldAssignment&lt;TInstance, TProp&gt; ( this Expression&lt;Func&lt;TInstance, TProp&gt;&gt; fieldGetter ) { return fieldGetter.ToFieldAssignExpression().Compile(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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