3

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.

Ahmet Firat Keler
  • 2,603
  • 2
  • 11
  • 22
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • Related (not quite duplicate): http://stackoverflow.com/questions/2998954/test-if-a-property-is-available-on-a-dynamic-variable – Cody Gray - on strike Jul 07 '16 at 06:14
  • 1
    You can cook up something using reflection, but there is a bit of a code smell in your question. Why is `Modified` not a property on `IAuditableTable`? – CompuChip Jul 07 '16 at 06:14
  • Possible duplicate of [Check if a property exist in a class](http://stackoverflow.com/questions/15341028/check-if-a-property-exist-in-a-class) – fubo Jul 07 '16 at 06:14
  • @CompuChip - sometime in the future it might be but for now I don't want to change 30+ tables and a lot of code :-) – Samantha J T Star Jul 07 '16 at 06:15
  • 1
    Use refelction var myproperty = table.GetType().GetProperty("modified") – Hakunamatata Jul 07 '16 at 06:15
  • 1
    You mean you don't want to fix your design now but later when there is even _more_ code depending on it? – CompuChip Jul 07 '16 at 06:15
  • 1
    Well you could always have another interface, `IModificationRecord` or something, and check whether `table` implements that interface... – Jon Skeet Jul 07 '16 at 06:16
  • @Hakunamatata - Thanks, this is what I was able to find also. But how can I set that property? table.modified will not work as a way to set it. – Samantha J T Star Jul 07 '16 at 06:20
  • @JonSkeet - I like your idea. Actually I was not aware I could have more than one interface at a time. Now I tried to code a solution using GetType but I came across a problem when trying to set the property. – Samantha J T Star Jul 07 '16 at 06:21
  • if(myproperty !=null && myproperty.CanWrite) { myproperty.SetValue(table, value, null); } https://msdn.microsoft.com/en-us/library/xb5dd1f1(v=vs.110).aspx – Hakunamatata Jul 07 '16 at 06:27

1 Answers1

8

Using reflection:

PropertyInfo propInfo 
    = table.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
    .FirstOrDefault(x => x.Name.Equals("modified ", StringComparison.OrdinalIgnoreCase));

// get value
if(propInfo != null)
{
    propInfo.SetValue(table, DateTime.Now);
}

Or as others have pointed out, you would better have your class implement another interface, eg IHasModified and:

if(table is IHasModified)
{
    (table as IHasModified).modified = //whatever makes you happy. ;
}
Zein Makki
  • 29,485
  • 6
  • 52
  • 63