How can I test if an object has a property and set it?
<p>I have this code in C#</p>
<pre><code>foreach (var entry in auditableTableEntries)
{
  IAuditableTable table = (IAuditableTable)entry.Entity;
  table.ModifiedBy = userId;
  table.ModifiedDate = dateTime;
  if (entry.State == EntityState.Added || entry.State == EntityState.Modified)
  {
    if (table.CreatedBy == null || table.CreatedBy == null)
    {
      table.CreatedBy = userId;
      table.CreatedDate = dateTime;
    }
  }
}
</code></pre>
<p>Some of the table objects have a property <code>modified</code> and for these I would like to set the property to the value of number of seconds. Since 1970.  Something like:</p>
<pre><code>table.modified = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds
</code></pre>
<p>But how can I tell if the table has that property? I don't want to set the property if it does not exist as I assume that would cause an exception.</p>
<p>Here's what I have tried so far:</p>
<pre><code>if (table.GetType().GetProperty(&quot;modified&quot;) != null)
{
  // The following line will not work as it will report that
  // an IAuditableTable does not have the .modified property
    
  table.modified = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds
}
</code></pre>
<p>But the problem with this is that table.modified is not valid syntax as IAuditableTable does not contain a modified property.</p>