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
Post a Comment