PropertyInfo.GetValue 在自定义类上失败

发布于 2024-12-27 14:49:53 字数 1603 浏览 3 评论 0原文

我有一个对象 RenderComponent,它有一个名为 Model 且类型为 StaticModel 的属性。当我获取 RenderComponentPropertyInfo 然后对其调用 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)

其中 infoStaticModelPropertyInfo >,并且 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:
enter image description here

And here's the error I get:
enter image description here

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:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

定格我的天空 2025-01-03 14:49:53

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

为什么 obj(在最后一个 Watch 屏幕截图中)有 Key &价值?不应该是 RenderComponent 吗?我认为您正在传递 KeyValuePair 而不是 RenderComponent 实例

sos00's comment from the original question was the correct answer to this question. His comment was:

Why does obj (in last Watch screenshot) has Key & Value? isn't supposed to be a RenderComponent ? I think you are passing a KeyValuePair instead of a RenderComponent instance

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文