title: Is it possible to declare an anonymous type in C# with a variable/dynamic set of fields?
body: <p>In C#, I would like to figure out if it's possible to declare an anonymous type where the fields are not known until run-time.</p>
  
  <p>For example, if I have a List of key/value pairs, can I declare an anonymous type based on the contents of that list?  The specific case I'm working with is passing parameters to Dapper, where I don't know ahead of time how many parameters I will have.</p>
  
  <pre><code>List&lt;Tuple&lt;string, string&gt;&gt; paramList = new List&lt;Tuple&lt;string, string&gt;&gt;() {
      new Tuple&lt;string, string&gt;("key1", "value1"),
      new Tuple&lt;string, string&gt;("key2", "value2")
      ...
  };
  </code></pre>
  
  <p>I'd like to convert this List (or an equivalent Map) into an anonymous type that I can pass to Dapper as query parameters.  So ideally, the above list would wind up looking like this, if defined as an anonymous type:</p>
  
  <pre><code>new { key1=value1, key2=value2, ... }
  </code></pre>
  
  <p>I've seen several questions on StackOverflow asking about <em>extending</em> anonymous types after they are declared ("extendo objects"), or declaring arbitrary fields on an object after it's created, but I don't need to do that... I just need to declare the types dynamically up-front once.  My suspicion is that it will require some fancy reflection, if it's possible at all.</p>
  
  <p>My understanding is that the compiler defines a type for anonymous classes under the hood at compile-time, so if the fields of that class are not available until run-time, I might be out of luck.  My use case may in fact be no different in actuality than using an "extendo object" to define arbitrary fields, whenever.</p>
  
  <p>Alternatively, if anyone knows of a better way to pass query parameters to Dapper (rather than declaring an anonymous class), I would love to hear about that as well.</p>
  
  <p>Thanks!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Sorry for the delay in getting back to this one!  These answers were all great, I wish I could give points to everyone.  I ended up using jbtule's solution (with edit by Sam Saffron), passing IDynamicParameters to Dapper, so I felt I had to give the answer to him.  The other answers were also good, and answered specific questions that I had asked. I really appreciate everyone's time on this!</p>