Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xna PropertyInfo.GetValue在自定义类上失败_Xna_C#_Reflection - Fatal编程技术网

Xna PropertyInfo.GetValue在自定义类上失败

Xna PropertyInfo.GetValue在自定义类上失败,xna,c#,reflection,Xna,C#,Reflection,我有一个对象,RenderComponent,它有一个名为Model的属性,类型为StaticModel。当我获取我的RenderComponent的PropertyInfo并对其调用GetValue()时,它给了我一个TargetException,表示“对象与目标类型不匹配” 我将编写一个小的代码示例,为您提供失败原因的要点。以下是层次结构: public class RenderComponent { StaticModel Model; } public class Stati

我有一个对象,
RenderComponent
,它有一个名为
Model
的属性,类型为
StaticModel
。当我获取我的
RenderComponent
PropertyInfo
并对其调用
GetValue()
时,它给了我一个TargetException,表示“对象与目标类型不匹配”

我将编写一个小的代码示例,为您提供失败原因的要点。以下是层次结构:

public class RenderComponent
{
    StaticModel Model;
}

public class StaticModel
{

}
我基本上是这样做的:

RenderComponent renderComponent = new RenderComponent();
PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

foreach (PropertyInfo info in props)
{
    // I get the exception here
    Object value = info.GetValue(renderComponent, null);
}
我的程序当然要复杂得多,但我仍然不明白它怎么会失败。我一定错过了一些简单的东西

我在这一行得到了一个例外:

info.GetValue(obj,null)

其中,
info
StaticModel
属性info
obj
是一个实例化的
RenderComponent
。当我在这个异常处停止并观察obj时,我可以实际地逐步遍历层次结构,直到我得到我的StaticModel值,它存在并拥有我期望的数据

我敢肯定,如果我在这个线程中发布了一个小的代码示例,它会工作得很好,因此它必须与我在引擎中如何调用它有关,我只是不知道当obj显然在其中包含该值时,GetValue如何会失败

这是我调试“obj”并找到静态模型的截图:

下面是我得到的错误:

在这里,您可以看到
obj
info
的值,作为我传递的对象实例是RenderComponent的证据,并且我调用GetValue的PropertyInfo是一个静态模型(它是RenderComponent的成员,如您在第一幅图中所见:

sos00对原始问题的评论是对该问题的正确回答。他的评论是:

为什么obj(在上一次观看的屏幕截图中)有键和值?不应该是RenderComponent吗?我认为您传递的是键和值对,而不是RenderComponent实例


抱歉,
info
的值在哪里?即使您在第二次捕获中搜索了信息,但您两次都在显示obj。为什么obj(在上一次观看的屏幕截图中)会显示有Key&Value?不应该是RenderComponent吗?我想你传递的是KeyValuePair而不是RenderComponent instanceRight,传递的是obj.Value。@所以我想你可能是对的,我引擎中的组件在字典中。