title: How to dynamically convert List<MyClass> to List<KeyValue>?
body: <p>If I have, for example, this class</p>
  
  <pre><code>public class User{
      int Id;
      string Name;
      short Age;
  }
  </code></pre>
  
  <p>and a collection of <code>User</code>:</p>
  
  <pre><code>public class UserCollection: List&lt;User&gt;{...}
  </code></pre>
  
  <p>Can I create a <code>List&lt;KeyValue&gt;</code> (<code>KeyValue</code> is my class)  from an <code>UserCollection</code> choosing which property is the <code>Key</code> (or <code>Value</code>)?</p>
  
  <p>I tried with this code:</p>
  
  <pre><code>private List&lt;KeyValue&gt; ReadCollection(dynamic collection, string keyName, string valueName)
  {
      List&lt;KeyValue&gt; list = new List&lt;KeyValue&gt;();
  
      for(int i = 0; i &lt; collection.Count; i++)
      {
          dynamic elem = collection[i];
          // I can't use this notation, but only elem.Id
          int key = elem[keyName]; 
          // I can't use this notation, but only elem.Name
          string value = elem[valueName];
  
          list.Add(new KeyValue(key, value));
       }
  
       return list;
  }
  
  List&lt;KeyValue&gt; list = ReadCollection(userCollection, "Id", "Name");
  </code></pre>
  
  <p>but it doesn't work. It is possible to choose which property put inside my <code>KeyValue</code> class?</p>