title: How to handle null using refelection
body: <p>I am trying to set the value of object. I get the error because of <code>Object of type 'System.DBNull' cannot be converted to type 'System.Nullable1[System.DateTime]'</code> a. How to handle null using reflection.</p>
  
  <p>I tried to solve like in described at the link but it won't help me.</p>
  
  <p>I got this
  <a href="http://technico.qnownow.com/how-to-set-property-value-using-reflection-in-c/" rel="nofollow">How to set property value using reflection in C#</a></p>
  
  <pre><code>    protected TEntity ToFirst(IDbCommand command)
      {
          using (var record = command.ExecuteReader())
          {
              var items = new TEntity();
              while (record.Read())
              {
                  items = Map&lt;TEntity&gt;(record);
              }
              return items;
          }
      }
  
      protected TEntity Map&lt;TEntity&gt;(IDataRecord record)
      {
          var objT = Activator.CreateInstance&lt;TEntity&gt;();
          foreach (var prop in typeof(TEntity).GetProperties())
          {
              var attributes = prop.GetCustomAttributes(false);
              var columnMapping = attributes.FirstOrDefault(a =&gt; a.GetType() == typeof(DbColumnAttribute));
              if (columnMapping != null)
              {
                  var map = columnMapping as DbColumnAttribute;
                  if (map != null &amp;&amp; (record.HasColumn(map.Name) &amp;&amp; record.IsDBNull(record.GetOrdinal(map.Name))))
                      prop.SetValue(objT, record[prop.Name]);
              }
              else
              {
                  if (record.HasColumn(prop.Name) &amp;&amp; record.IsDBNull(record.GetOrdinal(prop.Name)))
                      prop.SetValue(objT, record[prop.Name]);
              }
          }
          return objT;
      }
  </code></pre>