title: Object does not match target type exception when using a base class list
body: <p>I have three classes that are setup as follows. I want to get an array of the property <strong>values</strong> from the base class for all of the subclasses in the list of a base type. </p>
  
  <pre><code>public abstract class Base
  {
    ...   
  }
  
  public class A : Base
  {
    ...
  }
  
  public class B : Base
  {
    ...
  }
  </code></pre>
  
  <p>So my code looks like this where <code>list1&lt;Base&gt;</code> is a list containing elements of both type <code>A</code> and <code>B</code>:</p>
  
  <pre><code>List&lt;PropertyInfo&gt; props = new List&lt;PropertyInfo&gt;();
  foreach(property in typeOf(Base).GetProperties())
  {
     props.add(property)
  }
  
  foreach (var ele in list1)
  {
     var propsArray = props.Select(prop =&gt; prop.GetValue(ele, null)).ToArray();
     ...
  }
  </code></pre>
  
  <p>It has no problem for the first item it encounters, whether it is <code>A</code> or <code>B</code>, but then it has a problem with the second if it is the opposite type.</p>
  
  <p>Here is the stack trace.</p>
  
  <pre><code>System.Reflection.TargetException: Object does not match target type.
   at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr,    Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder   binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
  </code></pre>
  
  <p>Any help is greatly appreciated!</p>