Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Here is a full solution:</strong></p> <pre><code>// Credits to digEmAll for the following code public delegate void Setter&lt;T&gt;(T newValue); public delegate T Getter&lt;T&gt;(); public class MagicPointer&lt;T&gt; { private Getter&lt;T&gt; getter; private Setter&lt;T&gt; setter; public T Value { get { return getter(); } set { setter(value); } } public MagicPointer(Getter&lt;T&gt; getter, Setter&lt;T&gt; setter) { this.getter = getter; this.setter = setter; } } // Code starting from here is mine public static class MagicUtilClass { public static MagicPointer&lt;T&gt; LinkVariable&lt;T&gt;(Expression&lt;Func&lt;T&gt;&gt; expression) { var memberExpr = expression.Body as MemberExpression; if (memberExpr == null) throw new InvalidOperationException("The body of the expression is expected to be a member-access expression."); var field = memberExpr.Member as FieldInfo; if (field == null) throw new InvalidOperationException("The body of the expression is expected to be a member-access expression that accesses a field."); var constant = memberExpr.Expression as ConstantExpression; if (constant == null) throw new InvalidOperationException("The body of the expression is expected to be a member-access expression that accesses a field on a constant expression."); return new MagicPointer&lt;T&gt;(() =&gt; (T) field.GetValue(constant.Value), x =&gt; field.SetValue(constant.Value, x)); } } </code></pre> <p><strong>Usage:</strong></p> <pre><code>int x = 47; var magic = MagicUtilClass.LinkVariable(() =&gt; x); magic.Value = 48; Console.WriteLine(x); // Outputs 48 </code></pre> <p><strong>To understand why this solution works,</strong> you need to know that the compiler transforms your code quite considerably whenever you use a variable inside a lambda expression (irrespective of whether that lambda expression becomes a delegate or an expression tree). It actually generates a new class containing a field. The variable <em>x</em> is removed and replaced with that field. The Usage example will then look something like this:</p> <pre><code>CompilerGeneratedClass1 locals = new CompilerGeneratedClass1(); locals.x = 47; var magic = MagicUtilClass.LinkVariable(() =&gt; locals.x); // etc. </code></pre> <p>The “field” that the code retrieves is the field containing <em>x</em>, and the “constant” that it retrieves is the <em>locals</em> instance.</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