c# - Object does not match target type exception when using a base class list -


i have 3 classes setup follows. want array of property values base class of subclasses in list of base type.

public abstract class base {   ...    }  public class : base {   ... }  public class b : base {   ... } 

so code looks list1<base> list containing elements of both type a , b:

list<propertyinfo> props = new list<propertyinfo>(); foreach(property in typeof(base).getproperties()) {    props.add(property) }  foreach (var ele in list1) {    var propsarray = props.select(prop => prop.getvalue(ele, null)).toarray();    ... } 

it has no problem first item encounters, whether a or b, has problem second if opposite type.

here stack trace.

system.reflection.targetexception: object not match target type.  @ system.reflection.runtimemethodinfo.checkconsistency(object target)  @ system.reflection.runtimemethodinfo.invokeargumentscheck(object obj, bindingflags invokeattr,    binder binder, object[] parameters, cultureinfo culture)  @ system.reflection.runtimemethodinfo.invoke(object obj, bindingflags invokeattr, binder   binder, object[] parameters, cultureinfo culture)  @ system.reflection.runtimepropertyinfo.getvalue(object obj, bindingflags invokeattr, binder binder, object[] index, cultureinfo culture)  @ system.reflection.runtimepropertyinfo.getvalue(object obj, object[] index) 

any appreciated!


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -