title: Looping through properties conditionally using reflection
body: <p>I am using a web service to return me a <code>JsonAddressModel</code> in return for a uprn <code>string</code> within my application in various places.</p>
  
  <p>I have a requirement to print a customers address as a concatenated <code>string</code> on a confirmation page of my form.</p>
  
  <p>I initially wrote this method, which is designed to comma separate each property and omit any which are null, which works well however is admittedly quite repetitive.</p>
  
  <p><strong>Original Method</strong></p>
  
  <pre><code>public static string ConcatAddress(JsonAddressModel jsonAddressModel)
  {
      StringBuilder builder = new StringBuilder();
  
      if (jsonAddressModel.saon.Replace(" ", "") != "" &amp;&amp; jsonAddressModel != null)
          builder.Append(jsonAddressModel.saon + ", ");
  
      if (jsonAddressModel.paon != null)
          builder.Append(jsonAddressModel.paon + ", ");
  
      if (jsonAddressModel.thorofare != null)
          builder.Append(jsonAddressModel.thorofare + ", ");
  
      if (jsonAddressModel.dthorofare != null)
          builder.Append(jsonAddressModel.dthorofare + ", ");
  
      if (jsonAddressModel.posttown != null)
          builder.Append(jsonAddressModel.posttown + ", ");
  
      if (jsonAddressModel.county != null)
          builder.Append(jsonAddressModel.county + ", ");
  
      if (jsonAddressModel.postcode != null)
          builder.Append(jsonAddressModel.postcode);
  
      return builder.ToString();
  }
  </code></pre>
  
  <p>After testing this, I sought to refactor in a more elegant manner, and decided I would try and use reflection to work out the properties of <code>JsonAddressModel</code> and append each one in turn using a <code>foreach</code> loop.</p>
  
  <p><strong>New Method</strong></p>
  
  <pre><code>public static string ConcatAddress(JsonAddressModel jsonAddressModel)
  {
      StringBuilder builder = new StringBuilder();
  
      foreach (PropertyInfo prop in jsonAddressModel.GetType().GetProperties())
      {
          if (prop != null)
          {    
              builder.Append(prop.GetValue(jsonAddressModel, null) + ",");
          }
      }                
      return builder.ToString();
  }
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>Whilst the new method is now appending each of the correct properties that I want displayed such as the saon/paon, thorofare etc. It is also appending the eastings, northings, uprn as well as stuff like <code>System.Runtime.Serialization.ExtensionDataObject</code> which are obviously things I do not wish to include in my concatenated address string.</p>
  
  <p><strong>Question</strong></p>
  
  <p>Is there anyway of stipulating exactly which properties I allow to be appended here?</p>
  
  <p>I don't think <code>BindingFlags</code> will work in my instance because as mentioned previously I am receiving this model from a web service and both the properties I want, and don't want, respectively are <code>Private</code>.</p>
  
  <p>I've considered creating an Array however the only name related field I can see is the <code>SetMethod</code> which will be something like <code>SetMethod = {Void set_saon(System.String)}</code> but for each property, and I'm not fully sure how to get a handle on this.</p>