C# 使用基类列表时,对象与目标类型不匹配异常

C# 使用基类列表时,对象与目标类型不匹配异常,c#,inheritance,reflection,C#,Inheritance,Reflection,我有三个类,设置如下。我想从基类中为基类型列表中的所有子类获取属性值的数组 public abstract class Base { ... } public class A : Base { ... } public class B : Base { ... } 所以我的代码看起来像这样,list1是一个包含a和B类型元素的列表: List<PropertyInfo> props = new List<PropertyInfo>(); forea

我有三个类,设置如下。我想从基类中为基类型列表中的所有子类获取属性值的数组

public abstract class Base
{
  ...   
}

public class A : Base
{
  ...
}

public class B : Base
{
  ...
}
所以我的代码看起来像这样,
list1
是一个包含
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();
   ...
}

非常感谢您的帮助

如何设置道具?
prop
ele
的运行时类型不匹配。使用调试器进行确认。不要搞错:GetValue没有bug。论点是错误的。你没有真正表现出任何东西。没有类的成员,没有
列表1
,没有
道具
。。。我们应该猜猜吗?@wdavo我增加了设置道具的更新。我可以看到ele的运行时是预期的A或B,但这应该是可以的,对吗?一个简单的例子就可以了。也许是基本物质的实际性质。鉴于前面的评论,我认为您没有提供足够的信息让我们发现您的代码哪里有问题。
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)