PropertyInfo.GetValue 在自定义类上失败
我有一个对象 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
的 PropertyInfo
>,并且 obj
是一个实例化的 RenderComponent
。当我停止在这个异常上并在 obj 上进行监视时,我可以实际单步执行层次结构,直到到达我的 StaticModel 值,并且它存在并具有我期望的数据。
我确信如果我在这个线程中发布了小代码示例,它会工作得很好,所以它必须与我在引擎中调用它的方式有关,我只是不明白当 obj 时 GetValue 如何失败显然其中蕴藏着这样的价值。
这是我调试“obj”并找到 StaticModel 的屏幕截图:
这是我得到的错误:
在这里您可以看到 obj
和 的值>info
,作为我传递的对象实例是 RenderComponent 的证明,而我调用 GetValue 的 PropertyInfo 是静态模型(它是 RenderComponent 的成员,如您在第一张图片中看到的那样) :
I have an object, RenderComponent
, that has a property named Model
that is of type StaticModel
. When I get the PropertyInfo
of my RenderComponent
and then call GetValue()
on it, it is giving me a TargetException, saying "Object does not match target type."
I'll write a small sample of code to give you the gist of what is failing. Here's the hierarchy:
public class RenderComponent
{
StaticModel Model;
}
public class StaticModel
{
}
And I'm essentially doing this:
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);
}
My program is of course much more complicated, but I still don't see how it could be failing. I must be missing something simple.
I get the exception on this line:
info.GetValue(obj, null)
Where info
is the PropertyInfo
of StaticModel
, and obj
is an instantiated RenderComponent
. When I stop at this exception and do a watch on obj, I can actual step through the hierarchy until I get to my StaticModel value, and it exists and has the data I would expect.
I'm sure if I took the small code sample I posted in this thread it would work fine, so it has to be something with how I'm calling it within my engine, I just don't see how GetValue can fail when obj clearly has that value within it.
Here's a screenshot of me debugging 'obj' and finding the StaticModel:
And here's the error I get:
And here you can see the values of obj
and info
, as proof that my object instance I'm passing is a RenderComponent, and the PropertyInfo I'm calling GetValue on is a Static Model (which is a member of RenderComponent, as you can see in the first image:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sos00 对原始问题的评论是该问题的正确答案。他的评论是:
sos00's comment from the original question was the correct answer to this question. His comment was: