title: TargetException thrown in Reflection using GetValue() in nested properties
body: <p>I need to get the <em>Name</em> and <em>Value</em> of all properties of each object. Some of them are reference type so if I get the following objects:</p>
  <pre><code>public class Artist {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  
  public class Album {
      public string AlbumId { get; set; }
      public string Name { get; set; }
      public Artist AlbumArtist { get; set; }
  }
  </code></pre>
  <p>When getting the properties from the <code>Album</code> object I would need also to get the values of properties <code>AlbumArtist.Id</code> and <code>AlbumArtist.Name</code> which are nested.</p>
  <p>I have the following code so far, but it triggers the <strong>System.Reflection.TargetException</strong> when trying to get the value of the nested ones.</p>
  <pre><code>var valueNames = new Dictionary&lt;string, string&gt;();
  foreach (var property in row.GetType().GetProperties())
  {
      if (property.PropertyType.Namespace.Contains(&quot;ARS.Box&quot;))
      {
          foreach (var subProperty in property.PropertyType.GetProperties())
          {
              if(subProperty.GetValue(property, null) != null)
                  valueNames.Add(subProperty.Name, subProperty.GetValue(property, null).ToString());
          } 
      }
      else
      {
          var value = property.GetValue(row, null);
          valueNames.Add(property.Name, value == null ? &quot;&quot; : value.ToString());
      }
  }
  </code></pre>
  <p>So in the <code>If</code> statement I just check if the property is under the namespace of my reference types, if it is I should get all the nested properties values but that's where the exception is raised.</p>