title: How do I deal with arrays using reflection
body: <p>I am writing some validation code. The code will take data passed into a web service and decide whether it can do the action, or return a message to the caller that they have missed out some fields etc.</p>
  
  <p>I have it mostly working except for arrays. I am marking up the properties with a [RequiredField] attribute to represent fields that are required. So if this is some of my data,</p>
  
  <pre><code>public enum EnumTest
  {
      Value1,
      Value2
  }
  
  [DataContract]
  public class DummyWebserviceData
  {
      [DataMember]
      [RequiredField]
      public EnumTest[] EnumTest{ get; set; }
  
      [DataMember]
      [RequiredField]
      public DummyWebserviceData2[] ArrayOfData { get; set; }
  }
  
  [DataContract]
  public class DummyWebserviceData2
  {
      [DataMember]
      [RequiredField]
      public string FirstName { get; set;}
  
      [DataMember]
      [RequiredField]
      public string LastName { get; set;}
  
      [DataMember]
      public string Description { get; set;}
  }
  </code></pre>
  
  <p>So what do I have working? I have validation of dates, and strings working. It uses recursion to go any level deep required on the data.</p>
  
  <p>But ... so what about the two arrays there. The first is an array of enums. I want to check in that case that the array is not empty.</p>
  
  <p>The second is the array of DummyWebserviceData2 values. I need to pull each value out and have a look at it individually.</p>
  
  <p>To simplify the code I have written it looks something like this,</p>
  
  <pre><code>foreach (PropertyInfo propertyInfo in data.GetType().GetProperties())
  {
      if (propertyInfo.PropertyType.IsArray)
      {
          // this craps out
  
          object[] array = (object[])propertyInfo.GetValue(data, new object[] { 0 });
  
      }
  }
  </code></pre>
  
  <p>So it seems to me that the first thing is that I can tell it is an array. But how then can I tell how many items are in the array?</p>