Note that there are some explanatory texts on larger screens.

plurals
  1. POC# - Get property in member class using Reflection
    primarykey
    data
    text
    <p><strong>SHORT VERSION</strong></p> <p>What's the best way to use reflection to turn something like <code>string prop = "part1.first_name";</code> into a <code>System.Reflection.PropertyInfo</code>, so that I can use the GetValue and SetValue functions?</p> <p><strong>LONG VERSION</strong></p> <p>I'm using ASP .NET MVC to build a questionnaire for my organization. It's very long, so it's divided into several different pages. Since it's not uncommon for us to get requests like, "Can you move this question to that page, and this other question to another page," I need to build this to be pretty flexible for a junior programmer to change.</p> <p>My model is a complex class (it's got five member classes that have mostly primitive-typed properties on them).</p> <p>So, I access it by doing things like <code>Model.part1.first_name</code> or <code>Model.part2.birth_date</code>.</p> <p>Since the same model is used on all of the pages, but not all of the questions are on every page, I have ActionAttributes that essentially clear out all of the properties that were submitted on the form <em>except</em> for the ones that were displayed on that page (so someone can't inject a hidden field into the form and have the value persist to the database).</p> <p>I want to make sure that I only save valid field values and don't let the user proceed to the next page until the current one is entirely OK, but I also want to save the values that <em>are</em> valid, even if the user isn't allowed to proceed.</p> <p>To do this, I have a function that takes two instances of my model class, a reference to the ModelStateDictionary, and a <code>string[]</code> of field names like "part1.first_name" and "part2.birth_date". That function needs to copy all of the values listed in the string array that do not have validation errors from the first (ie, form-submitted) object into the second (ie, loaded from the db) object.</p> <p>As stated above, what's the best way to use reflection to turn something like "part1.first_name" into a <code>System.Reflection.PropertyInfo</code>, OR, is there a better way to accomplish this?</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