title: Using expression tree to navigate and return an object that owns a property
body: <p>Given an expression that points to a property in an object graph, I want to retrieve the instance that owns the property withing that graph.  I've been struggling. The current code just gives me  System.InvalidOperationException : Lambda Parameter not in scope</p>
  
  <pre><code>    [Test]
      public void TestExpression()
      {
          var person = new Person {Address = {Street = "Bank Street"}, Name = "Joe"};
  
          Expression&lt;Func&lt;Person, object&gt;&gt; exp = p =&gt; p.Address.Street;
          Assert.AreEqual("Bank Street", exp.Compile().Invoke(person));
  
          var owner = GetPropertyOwner(person, exp);
          Assert.AreEqual(owner, person.Address);
      }
  
      private object GetPropertyOwner&lt;T&gt;(T root, Expression&lt;Func&lt;T, object&gt;&gt; exp)
      {
          if (exp.Body is MemberExpression)
          {
              var member = exp.Body as MemberExpression;
              if (member.Expression is MemberExpression)
              {
                  var parentMember = member.Expression as MemberExpression;
                  //parent member will be {p.Address}
                  //Now I'm trying to reconstruct an expression that I can combile
                  var parameterExpression = System.Linq.Expressions.Expression.Parameter(typeof(T), "p");
                  var lambdaExpression = System.Linq.Expressions.Expression.Lambda(parentMember, parameterExpression);
                  //Next Linethrows  System.InvalidOperationException : Lambda Parameter not in scope
                  var found = lambdaExpression.Compile().DynamicInvoke(person);
                  return found;
              }
          }
  
          return new object();
      }
  </code></pre>