Search code examples
c#reflectiondto

Reflection from DTO


I have 1 dto, statEMailDTO, which has a field that holds the Field Names of what I'm looking for (they are comma delimited.

var emailParams = statEmailDTO.EmailParam.ToString().Split(',');

for (int i = 0; i < emailParams.Length; i++) {
  var fieldName = emailParams[i].ToString();

etc.

But, then how do I use Reflection to then get the Actual value of ``fieldName which is found in a different DTO, siDTO.

So let's say that fieldName = "SuggestionItemID", I then what to get the value of siDTO.SuggestionItemID.

I haven't done a lot of reflection in the past. Sure, I read up on PropertyInfo, but it's just not clicking.

Thoughts?


Solution

  • Like this:

    PropertyInfo property = typeof(SomeType).GetProperty(fieldName);
    object value = property.GetValue(instance, null);