Note that there are some explanatory texts on larger screens.

plurals
  1. POPropertyInfo.GetValue failing on a custom class
    primarykey
    data
    text
    <p>I have an object, <code>RenderComponent</code>, that has a property named <code>Model</code> that is of type <code>StaticModel</code>. When I get the <code>PropertyInfo</code> of my <code>RenderComponent</code> and then call <code>GetValue()</code> on it, it is giving me a TargetException, saying "Object does not match target type."</p> <p>I'll write a small sample of code to give you the gist of what is failing. Here's the hierarchy:</p> <pre><code>public class RenderComponent { StaticModel Model; } public class StaticModel { } </code></pre> <p>And I'm essentially doing this:</p> <pre><code>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); } </code></pre> <p>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.</p> <p>I get the exception on this line:</p> <p><code>info.GetValue(obj, null)</code></p> <p>Where <code>info</code> is the <code>PropertyInfo</code> of <code>StaticModel</code>, and <code>obj</code> is an instantiated <code>RenderComponent</code>. 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.</p> <p>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.</p> <p><strong>Here's a screenshot of me debugging 'obj' and finding the StaticModel:</strong> <img src="https://i.stack.imgur.com/NMZNq.jpg" alt="enter image description here"></p> <p><strong>And here's the error I get:</strong> <img src="https://i.stack.imgur.com/GZdTF.jpg" alt="enter image description here"></p> <p><strong>And here you can see the values of <code>obj</code> and <code>info</code>, 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:</strong> <img src="https://i.stack.imgur.com/FXOrM.jpg" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload