title: Implementing validation rules inside of class
body: <p>I have <code>class</code> like this</p>
  
  <pre><code>public class EmployeeInfo
  {
      public string EmployeeName { get; set; }
      public string PhoneNumber { get; set; }
      public string Office { get; set; }
      public string Department { get; set; }
  }
  </code></pre>
  
  <p>I've created method that takes <code>SPListItemCollection</code> as parameter and returns list of object of <code>EmployeeInfo</code> class.There is no restrictions inside <code>SPList</code> and when you create new item you can leave some of the fields empty, so I've used <code>reflection</code> to determine if field is <code>null</code> and then insert empty string when populating <code>EmployeeInfo</code>object to avoid exceptions.</p>
  
  <pre><code> public List&lt;Models.EmployeeInfo&gt; GetEmployeeInfo(SPListItemCollection splic)
   {
  
          var listEmployeeInfo = new List&lt;Models.EmployeeInfo&gt;();
          var propertyNames = new List&lt;string&gt;() { "EmployeeName", 
                                                   "Department", 
                                                   "Office", 
                                                   "PhoneNumber"};
  
          foreach (SPListItem item in splic)
          {
              var employeeInfo = new Models.EmployeeInfo();
  
  
              foreach (var propertyName in propertyNames)
              {
                  string newData = "";
                  if (item[propertyName] != null)
                  {
                      newData = item[propertyName].ToString();
                  }
  
                  employeeInfo.GetType().GetProperty(propertyName).SetValue(employeeInfo, newData, null);
  
  
              }
              listEmployeeInfo.Add(employeeInfo);
          }
  
          return listEmployeeInfo;
  
      }
  </code></pre>
  
  <p>Later I've learned that it is bad practice to use <code>reflection</code> inside of nested loops , so I'm looking for different approach.
  Is there any chance I can make some pre Validation Rules inside <code>EmpployeeInfo</code> class , something like Validation method or to write some code inside class properties and than inside of <code>GetEmployeeInfo</code> method just to populate properties by calling that method?
  Thank you.</p>