title: C# - Use Reflection to Extract List of Values from a List<Class>
body: <p>I have a List which contains a class and would like to be able to use reflection to iterate through each property in that class and generate a List containing each property's values.  I have no problem using reflection to get all of the properties, but where I’m failing is in the syntax to iteratively extract a list of each property’s value.</p>
  
  <p>Example Code:</p>
  
  <pre><code>public class ExampleClass
  {
      public double Val1 {get; set;}
      public double Val2 { get; set; }
      public double Val3 { get; set; }
  
      public ExampleClass(double val1, double val2, double val3)
      {
          this.Val1 = val1;
          this.Val2 = val2;
          this.Val3 = val3 ;
      }
  }
  
  public class Main
  {
      public Main()
      {
          List&lt;ExampleClass&gt; exampleList = new List&lt;ExampleClass&gt;();
  
          exampleList.Add(new ExampleClass(1.1, 1.2, 1.3));
          exampleList.Add(new ExampleClass(2.1, 2.2, 2.3));
          exampleList.Add(new ExampleClass(3.1, 3.2, 3.3));
  
  
          List&lt;PropertyInfo&gt; properties = exampleList[0]
              .GetType()
              .GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
              .ToList();
  
          foreach (PropertyInfo prop in properties)
          {
              // Extract each property into a list, e.g.
              // 1st pass: list containing 1.1, 2.1, and 3.1
              // 2nd pass: list containing 1.2, 2.2 and 3.2
              // 3rd pass: list containing 1.3, 2.3 and 3.3
          }
      }
  }
  </code></pre>
  
  <p>If I was to manually specify the property I wanted I could use something like this:</p>
  
  <pre><code>var test = exampleList.Select(X =&gt; X.Val1).ToList();
  </code></pre>
  
  <p>Unfortunately, my best guesses look like the following and generate a “Object reference not set to an instance of an object” error</p>
  
  <pre><code>var test1 = collectionCompletionList.GetType().GetProperty(prop.Name).GetValue(collectionCompletionList, null);
  </code></pre>
  
  <p>What (presumably) simple thing am I missing here?</p>