I have this code in C#
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;
}
}
}
Some of the table objects have a property modified
and for these I would like to set the property to the value of number of seconds. Since 1970. Something like:
table.modified = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds
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.
Here's what I have tried so far:
if (table.GetType().GetProperty("modified") != 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
}
But the problem with this is that table.modified is not valid syntax as IAuditableTable does not contain a modified property.