title: How to get property name and value from generic model with generic list?
body: <p>Using the following model as an example.</p>
  
  <pre><code>public class FooModel
  {
      public FooModel()
      {
          Bars= new List&lt;BarModel&gt;();
      }
  
      [ManyToMany]
      public IList&lt;BarModel&gt; Bars{ get; set; }
  }
  </code></pre>
  
  
  
  <pre><code>public class BarModel
  {
      public int Id { get; set; }
  }
  </code></pre>
  
  <p>I need to extrapolate the <code>List&lt;BarModel&gt;</code> from a <code>fooModel</code> object, and build up a <code>Dictionary&lt;string, object&gt;</code> from each BarModel in the list. </p>
  
  <hr>
  
  <p>Let's say I create the following object.</p>
  
  <pre><code>var fooModel = new FooModel();
  var bar1 = new BarModel {Id = 1};
  var bar2 = new BarModel {Id = 2};
  fooModel.Bars = new List&lt;BarModel&gt;{bar1,bar2};
  </code></pre>
  
  <p>And now I want to get all properties within Foo that have the <code>[ManyToMany]</code> attribute.</p>
  
  <pre><code>// First I call the method and pass in the model
  DoSomething(fooModel);
  </code></pre>
  
  
  
  <pre><code>// Next I extract some values (used elsewhere)
  public DoSomething&lt;TModel&gt;(IModel model){
  
      var dbProvider = ...;
      var mapper = new AutoMapper&lt;TModel&gt;();
      var tableName = GetTableName( typeof( TModel ) );
  
      UpdateJoins( dbProvider, fooModel, tableName, mapper );
  }
  </code></pre>
  
  
  
  <pre><code>// Finally I begin dealing with the collection.
  private static void UpdateJoins&lt;TModel&gt; ( IDbProvider dbProvider, TModel model, string tableName, IAutoMapper&lt;TModel&gt; mapper ) where TModel : class, new()
  {
      foreach (
          var collection in
               model.GetType()
                    .GetProperties()
                    .Where( property =&gt; property.GetCustomAttributes( typeof( ManyToManyAttribute ), true ).Any() ) )
      {
  
          if ( !IsGenericList( collection.PropertyType ) )
              throw new Exception( "The property must be a List" );
  
          // Stuck Here - pseudo code
          //====================
          foreach (loop the collection)
  
               var collectionName = ...;  // Bar
               var nestedPropertyName = ...;  // Id
               var rightKey = collectionName + nestedPropertyName; // BarId
               var nestedPropertyValue = ...; // 1
  
      }
  }
  </code></pre>
  
  <p>In the example above, the OUTER <code>foreach</code> is only going to run ONCE because there is only one Property within <code>FooModel</code> that is decorated with the <code>[ManyToMany]</code> attribute.</p>
  
  <p>Therefore <code>PropertyInfo property</code> is a <code>List&lt;BarModel&gt;</code></p>
  
  <p>How do I do the above INNER <code>foreach</code> and extract the required data?</p>