title: Using nested collections of lambda expressions to create an object graph
body: <p>I am interested in utilizing lambda expressions to create a tree of property selectors.</p>
  
  <p>The usage scenario is that we have some code that does some recursive reflection on an object graph, and to limit the scope of recursion, we're currently using Attributes to mark which properties should be traversed. i.e. Get all decorated properties of object, if that property is a reference type with decorated properties, repeat for each of those too.</p>
  
  <p>The limitation of using Attributes is that you can only place them on Types you control the source for. A tree of lambda expressions allows the scope to be defined on any arbitrary type's public members. </p>
  
  <p>It would be handy to have a shorthand way to define these expressions, which reflects the structure of the object graph. </p>
  
  <p>Ultimately, I'd love to have something like this:</p>
  
  <pre><code>Selector&lt;MyType&gt; selector = new [] {
          (t =&gt; Property1),
          (t =&gt; Property2)
          {
                  p =&gt; NestedProperty1,
                  p =&gt; NestedProperty2
          }
  };
  </code></pre>
  
  <p>Right now, the best I can do declares an instance for each node explicitly something like this:</p>
  
  <pre><code>var selector = new Selector&lt;MyType&gt;()
  {
      new SelectorNode&lt;MyType, Property1Type&gt;(t =&gt; Property1),
      new SelectorNode&lt;MyType, Property2Type&gt;(t =&gt; Property2)
      {
          new SelectorNode&lt;Property2Type, NestedProperty1Type&gt;(p =&gt; NestedProperty1),
          new SelectorNode&lt;Property2Type, NestedProperty2Type&gt;(p =&gt; NestedProperty2)
      },
  };
  </code></pre>
  
  <p>There's nothing wrong with this code, but you have to write out the type arguments for each node explicitly, since the compiler can't infer the type arguments. This is a pain. And ugly. I've seen some incredible syntactical sugar out there, and am sure there must be a better way.</p>
  
  <p>Owing to my lack of understanding of 'higher' C# concepts like dynamics, co/contravariant generics and expression trees, I thought I'd pitch the question out there and see if any gurus know of a way to achieve this (or something rather like it?)</p>
  
  <hr>
  
  <p>For reference, these are the  declarations for the <code>Selector</code> and <code>SelectorNode</code> classes that achieve the structure I described in my post:</p>
  
  <pre><code>public interface ISelectorNode&lt;T&gt; {}
  
  public class Selector&lt;T&gt;: List&lt;ISelectorNode&lt;T&gt;&gt;{}
  
  public class SelectorNode&lt;T, TOut&gt;: List&lt;ISelectorNode&lt;TOut&gt;&gt;, ISelectorNode&lt;T&gt; 
  {
      public SelectorNode(Expression&lt;Func&lt;T, TOut&gt;&gt; select) {}
  }
  
  //Examples of Usage below
  
  public class Dummy
  {
      public ChildDummy Child { get; set; }
  }
  
  public class ChildDummy
  {
      public string FakeProperty { get; set; }
  }
  
  public class Usage
  {
      public Usage()
      {
          var selector = new Selector&lt;Dummy&gt;
          {
              new SelectorNode&lt;Dummy, ChildDummy&gt;(m =&gt; m.Child)
              {
                  new SelectorNode&lt;ChildDummy, string&gt;(m =&gt; m.FakeProperty)
              }
          };
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Edited in the interest of expanding on nawal's answer:</p>
  
  <p>Leveraging C#'s collection initializer syntax, we can get code to look like:</p>
  
  <pre><code>var selector = new Selector&lt;Dummy&gt;
    {
        (m =&gt; m.Child),
        {dummy =&gt; dummy.Child, 
            c =&gt; c.FakeProperty,
            c =&gt; c.FakeProperty                    
        }
    };
  </code></pre>
  
  <p>This is if our SelectorNode class' Add method looks like:</p>
  
  <pre><code>public class Selector&lt;T&gt; : List&lt;ISelectorNode&lt;T&gt;&gt;
  {
      public SelectorNode&lt;T, T, TOut&gt; Add&lt;TOut&gt;(Expression&lt;Func&lt;T, TOut&gt;&gt; selector, params Expression&lt;Func&lt;TOut, object&gt;&gt;[] children)
      {
          return SelectorNode&lt;T, T, TOut&gt;.Add(this, this, selector);
      }
  }
  </code></pre>
  
  <p>There must be a way to leverage this syntax!</p>