C# Get object inside of object that is a generic
<p>I have several View Models that contain a address object.  That address object of course has address1, address2, city, state, and zip.<br>
We are using the postal code address verification system, and I want all of my developers to be able to call a helper class, that will sniff View Model object for a address object.  If it does not find it, it will then check to see if the view model has basic address1, address2 and etc properties...  In either case I need it to get the address information for either the property object address or get the address properties... </p>

<p>So my helper class method signature looks like this:</p>

<pre><code> public void ShowVerificationWithReflection&lt;T&gt;(ModelStateDictionary modelState, T viewModel) where T : AMSBaseVM
</code></pre>

<p>I then do the following:</p>

<pre><code>var objType = viewModel.GetType();
List&lt;PropertyInfo&gt; properties = new List&lt;PropertyInfo&gt;();

properties.AddRange(objType.GetProperties());

foreach (PropertyInfo property in properties)
{
    if (property.CanRead)
    {
        if (property.Name == "Address1") testAddress.Address1 = property.GetValue(viewModel, null) as string;
        if (property.Name == "Address2") testAddress.Address2 = property.GetValue(viewModel, null) as string;
        if (property.Name == "City") testAddress.City = property.GetValue(viewModel, null) as string;
        if (property.Name == "StCd") testAddress.StateCodeId = (long)property.GetValue(viewModel, null);
        if (property.Name == "Zip") testAddress.Zip = property.GetValue(viewModel, null) as string;
    }
}
</code></pre>

<p>This works for the address properties that part of the View Model.
Now what I am stumbling with is detecting if the View Model has a property like this:</p>

<pre><code> public EntityAddressVM Address { get; set; }
</code></pre>

<p>I need to get that object from the generic and then get its address properties.  I have been able to find the object, but after that I get stuck...</p>

<pre><code> bool hasEntityAddress = objType.GetProperties().Any(p =&gt; p.PropertyType == typeof(EntityAddressVM));
</code></pre>

<p><strong>What I am needing help with is:</strong></p>

<ol>
<li><p>Determine if incoming viewModel (mvc) has a address object or has
address properties.</p></li>
<li><p>If it does have address Object, get the address properties otherwise get the address properties from the ViewModel.</p></li>
</ol>