title: Getting ConstantExpression.Value when actual value wrapped into DisplayClass because of closure
body: <p>Below is a simple demonstration code of my problem.</p>
  
  <pre><code>[TestClass]
  public class ExpressionTests
  {
      [TestMethod]
      public void TestParam()
      {
          Search&lt;Student&gt;(s =&gt; s.Id == 1L);
  
          GetStudent(1L);
      }
  
      private void GetStudent(long id)
      {
          Search&lt;Student&gt;(s =&gt; s.Id == id);
      }
  
      private void Search&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; filter)
      {
          var visitor = new MyExpressionVisitor();
          visitor.Visit(filter);
      }
  }
  
  public class MyExpressionVisitor : ExpressionVisitor
  {
      protected override Expression VisitConstant(ConstantExpression node)
      {
          Assert.AreEqual(1L, node.Value);
          return base.VisitConstant(node);
      }
  }
  </code></pre>
  
  <p><code>TestParam</code> method causes <code>VisitConstant</code> to be invoked on two different paths:</p>
  
  <p><strong>1.</strong> <code>TestParam</code> -> <code>Search</code> -> <code>VisitConstant</code></p>
  
  <p>In this execution path constant expression (1L) passed to <code>Search</code> method is a real constant value. Here, everything is OK, assert succeeds as expected. When <code>VisitConstant</code> is invoked via first path <code>node.Value.GetType()</code> is <code>Int64</code> and its <code>.Value</code> is <code>1L</code>.</p>
  
  <p><strong>2.</strong> <code>TestParam</code> -> <code>GetStudent</code> -> <code>Search</code> -> <code>VisitConstant</code></p>
  
  <p>In this execution path constant expression (id: 1L), is taken by <code>GetStudent</code> as an argument and passed to <code>Search</code> method inside a closure.</p>
  
  <p><strong>Problem</strong> </p>
  
  <p>The problem is on the second execution path. When <code>VisitConstant</code> is invoked via second path <code>node.Value.GetType()</code> is <code>MyProject.Tests.ExpressionTests+&lt;&gt;c__DisplayClass0</code> and  this class has a public field named <code>id</code> (same as <code>GetStudent</code> method's argument) which has the value of <code>1L</code>.</p>
  
  <p><strong>Question</strong></p>
  
  <p>How can I get <code>id</code> value in second path? I know about closures, what a <code>DisplayClass</code> is and why it is created at compile time etc. I am only interested in getting its field value.
  One thing I can think of is, via reflection. With something like below but it does not seem neat. </p>
  
  <pre><code>node.Value.GetType().GetFields()[0].GetValue(node.Value);
  </code></pre>
  
  <p><strong>Bonus Problem</strong></p>
  
  <p>While playing with the code for gettting <code>id</code> value I changed <code>VisitConstant</code> method like below (which will not solve my problem though) and get an exception saying <em>"'object' does not contain a definition for 'id'"</em></p>
  
  <p><img src="https://i.stack.imgur.com/Qmn3c.png" alt="enter image description here"></p>
  
  <p><strong>Bonus Question</strong></p>
  
  <p>As dynamics are resolved at runtime and <code>DisplayClass</code> is created at compile time, why cannot we access its fields with <code>dynamic</code>? While below code works, I expected that code would work too.</p>
  
  <pre><code>var st = new {Id = 1L};
  object o = st;
  dynamic dy = o;
  Assert.AreEqual(1L, dy.Id);
  </code></pre>