Ah, so that's reflection!

Reflection in .NET is something I’d heard of but never knew what it was. I’d also never thought to find out. That all changed when working on a .NET MVC project recently.

One of the requirements for the project I was working on was to create an audit trail of user actions including fields that had been updated. This wasn’t a problem for small records where only 1 or 2 fields were editable by the user. I would simply compare the new value with the previous value however I would have to have an if statement for each field in the class. Obviously this isn’t DRY so there had to be a better way.

After some investigation I discovered that it was possible to loop through the properties of an object. This meant I could just compare the previous and new values of each field in a foreach loop. This was a much cleaner solution to my problem especially as I now had some object with around 9 properties which were editable by the user. Using the previous method would have caused me to have to write 9 individual if statements, not clean at all and very repetitive.

The code I ended up writing was as follows:

private void AuditDetailUpdate(ref DetailRecord original, DetailRecord updated, ref bool objUpdated)  
            var props = typeof(DetailRecord).GetProperties();
            foreach (var p in props)
                Type t = p.PropertyType;
                //Only check strings, ints etc..
                if (t.IsPrimitive || t.IsValueType || (t == typeof(string)))
                    Object fieldOriginal = p.GetValue(original);
                    Object fieldUpdated = p.GetValue(updated);

                    if ((fieldOriginal != null) && !fieldOriginal.Equals(fieldUpdated))
                        objUpdated = true;
                        p.SetValue(original, fieldUpdated);
                        auditRepository.UpdateAdditionalHour(currentUser, original.Id, p.Name, fieldOriginal.ToString(), fieldUpdated.ToString());

A brief overview of what’s happening is as follows:


comments powered by Disqus