private void AddDataStoreItemAlterations(IDataStoreItem item) { foreach (PropertyInfo p in item.ConcreteType.GetProperties()) { System.Diagnostics.Debug.WriteLine("p.Name = " + p.Name); Attribute[] attrs = Attribute.GetCustomAttributes(p); foreach (System.Attribute attr in attrs) { System.Diagnostics.Debug.WriteLine("attr.ToString() = " + attr.ToString()); if (attr is AuditableAttribute) { Type t = p.PropertyType; var oldVal = db.Entry(item).OriginalValues.GetValue <object>(p.Name); PropertyInfo pi = item.GetType().GetProperty(p.Name); if (oldVal == null) { System.Diagnostics.Debug.WriteLine("oldVal IS null"); DataStoreItemAlteration d = new DataStoreItemAlteration(); d.ItemIDNumber = item.ID; d.Workstation = Platform.CurrentWorkstation; d.PropertyName = p.Name; d.OldValue = ""; if (pi.GetValue(item, null) == null) { d.NewValue = ""; } else { d.NewValue = pi.GetValue(item, null).ToString(); d.DataStoreItemName = item.ConcreteType.Name; db.DataStoreItemAlterations.Add(d); } } else if (pi.GetValue(item, null).ToString() != oldVal.ToString()) { System.Diagnostics.Debug.WriteLine("oldVal is Not null"); DataStoreItemAlteration d = new DataStoreItemAlteration(); d.ItemIDNumber = item.ID; d.Workstation = Platform.CurrentWorkstation; d.PropertyName = p.Name; d.OldValue = oldVal.ToString(); d.NewValue = pi.GetValue(item, null).ToString(); d.DataStoreItemName = item.ConcreteType.Name; db.DataStoreItemAlterations.Add(d); } } } } }
private void AddDataStoreItemAlterations(IDataStoreItem item) { foreach (PropertyInfo p in item.ConcreteType.GetProperties()) { if (p.Name == "LastEditDate") { continue; } if (!p.CanWrite) { continue; } if (p.PropertyType.IsPrimitive || p.PropertyType == typeof(string) || p.PropertyType == typeof(DateTime)) { Type t = p.PropertyType; var oldVal = db.Entry(item).OriginalValues.GetValue <object>(p.Name); PropertyInfo pi = item.GetType().GetProperty(p.Name); if (oldVal == null) { DataStoreItemAlteration d = new DataStoreItemAlteration(); d.ItemIDNumber = item.ID; d.Workstation = null; d.PropertyName = p.Name; d.OldValue = ""; if (pi.GetValue(item, null) == null) { d.NewValue = ""; } else { d.NewValue = pi.GetValue(item, null).ToString(); } d.DataStoreItemName = item.ConcreteType.Name; db.DataStoreItemAlterations.Add(d); } else if (pi.GetValue(item, null).ToString() != oldVal.ToString()) { DataStoreItemAlteration d = new DataStoreItemAlteration(); d.Workstation = null; d.PropertyName = p.Name; d.OldValue = oldVal.ToString(); d.NewValue = pi.GetValue(item, null).ToString(); d.DataStoreItemName = item.ConcreteType.Name; db.DataStoreItemAlterations.Add(d); } } } }
private object getValue(IDataStoreItem item, string property) { //System.Diagnostics.Debug.WriteLine("finding property " + property + " on " + item.ConcreteType.Name); Type t = item.GetType(); if (!property.Contains(">")) { var p = t.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.Name == property).First(); if (p.GetValue(item) == null) { //System.Diagnostics.Debug.WriteLine("Returning string.empty"); return(null); } //System.Diagnostics.Debug.WriteLine("Returning " + p.GetValue(item).ToString()); return(p.GetValue(item)); } else { var s = property.Split('>'); string typeName = s[0]; string prName = property.Remove(0, typeName.Length + 1); //System.Diagnostics.Debug.WriteLine("typeName = " + typeName + ", prName = " + prName); var p = t.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.Name == typeName).First(); if (p.GetValue(item) == null) { //System.Diagnostics.Debug.WriteLine("Returning string.empty"); return(null); } var obj = p.GetValue(item); //System.Diagnostics.Debug.WriteLine("obj is " + (obj as IDataStoreItem).ConcreteType.Name); //obj will be the IDataStoreItem that matches typeName if (s.Length > 2) { //System.Diagnostics.Debug.WriteLine("s.Length > 2"); string newString = ""; for (int i = 1; i < s.Length; i++) { newString = s[i] + ">"; } newString = newString.Remove(newString.Length - 1); //System.Diagnostics.Debug.WriteLine("newString = " + newString); return(getValue(obj as IDataStoreItem, prName)); } else { //System.Diagnostics.Debug.WriteLine("s.Length !> 2"); var y = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.Name == prName).First(); if (y.GetValue(obj) == null) { System.Diagnostics.Debug.WriteLine("Returning string.empty"); return(null); } //System.Diagnostics.Debug.WriteLine("Returning " + y.GetValue(obj).ToString()); return(y.GetValue(obj)); } } }