title: How do I create and access a new instance of an Anonymous Class passed as a parameter in C#?
body: <p>I have created a function that takes a SQL command and produces output that can then be used to fill a List of class instances.  The code works great.  I've included a slightly simplified version without exception handling here just for reference - skip this code if you want to jump right the problem. If you have suggestions here, though, I'm all ears.</p>
  
  <pre><code>    public List&lt;T&gt; ReturnList&lt;T&gt;() where T : new()
      {
          List&lt;T&gt; fdList = new List&lt;T&gt;();
          myCommand.CommandText = QueryString;
          SqlDataReader nwReader = myCommand.ExecuteReader();
          Type objectType = typeof (T);
          FieldInfo[] typeFields = objectType.GetFields();
          while (nwReader.Read())
          {
              T obj = new T();
              foreach (FieldInfo info in typeFields)
              {
                  for (int i = 0; i &lt; nwReader.FieldCount; i++)
                  {
                      if (info.Name == nwReader.GetName(i))
                      {
                          info.SetValue(obj, nwReader[i]);
                          break;
                      }
                  }
              }
              fdList.Add(obj);
          }
          nwReader.Close();
          return fdList;
      }
  </code></pre>
  
  <p>As I say, this works just fine.  However, I'd like to be able to call a similar function with an <em>anonymous class</em> for obvious reasons.</p>
  
  <p>Question #1: it appears that I must construct an anonymous class <strong>instance</strong> in my call to my anonymous version of this function - is this right?  An example call is:</p>
  
  <pre><code>.ReturnList(new { ClientID = 1, FirstName = "", LastName = "", Birthdate = DateTime.Today });
  </code></pre>
  
  <p>Question #2: the anonymous version of my ReturnList function is below.  Can anyone tell me why the call to info.SetValue simply does nothing?  It doesn't return an error or anything but neither does it change the value of the target field.</p>
  
  <pre><code>    public List&lt;T&gt; ReturnList&lt;T&gt;(T sample) 
      {
          List&lt;T&gt; fdList = new List&lt;T&gt;();
          myCommand.CommandText = QueryString;
          SqlDataReader nwReader = myCommand.ExecuteReader();
          // Cannot use FieldInfo[] on the type - it finds no fields.
          var properties = TypeDescriptor.GetProperties(sample); 
          while (nwReader.Read())
          {
              // No way to create a constructor so this call creates the object without calling a ctor. Could this be a source of the problem?
              T obj = (T)FormatterServices.GetUninitializedObject(typeof(T)); 
              foreach (PropertyDescriptor info in properties)  
              {
                  for (int i = 0; i &lt; nwReader.FieldCount; i++)
                  {
                      if (info.Name == nwReader.GetName(i))
                      {
                          // This loop runs fine but there is no change to obj!!
                          info.SetValue(obj, nwReader[i]);
                          break;
                      }
                  }
              }
              fdList.Add(obj);
          }
          nwReader.Close();
          return fdList;
      }
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p><strong>Note:</strong> when I tried to use the FieldInfo array as I did in the function above, the typeFields array had zero elements (even though the objectType shows the field names - strange). Thus, I use TypeDescriptor.GetProperties instead.</p>
  
  <p>Any other tips and guidance on the use of reflection or anonymous classes are appropriate here - I'm relatively new to this specific nook of the C# language.</p>
  
  <p>UPDATE: I have to thank Jason for the key to solving this.  Below is the revised code that will create a list of anonymous class instances, filling the fields of each instance from a query.</p>
  
  <pre><code>   public List&lt;T&gt; ReturnList&lt;T&gt;(T sample)
     {
         List&lt;T&gt; fdList = new List&lt;T&gt;();
         myCommand.CommandText = QueryString;
         SqlDataReader nwReader = myCommand.ExecuteReader();
         var properties = TypeDescriptor.GetProperties(sample);
         while (nwReader.Read())
         {
             int objIdx = 0;
             object[] objArray = new object[properties.Count];
             foreach (PropertyDescriptor info in properties) 
                 objArray[objIdx++] = nwReader[info.Name];
             fdList.Add((T)Activator.CreateInstance(sample.GetType(), objArray));
         }
         nwReader.Close();
         return fdList;
     }
  </code></pre>
  
  <p>Note that the query has been constructed and the parameters initialized in previous calls to this object's methods. The original code had an inner/outer loop combination so that the user could have fields in their anonymous class that didn't match a field. However, in order to simplify the design, I've decided not to permit this and have instead adopted the db field access recommended by Jason.  Also, thanks to Dave Markle as well for helping me understand more about the tradeoffs in using Activator.CreateObject() versus GenUninitializedObject.</p>