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:

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