private static string AutoLogRelationshipCreate <T>(T obj, Type type) { if (obj == null) { throw new ArgumentNullException("Parametro 'obj' não pode ser nulo"); } if (type == null) { throw new ArgumentNullException("Parametro 'type' não pode ser nulo"); } List <PropertyInfo> propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where( prop => Attribute.IsDefined(prop, typeof(LoggableRelationshipAttribute))).ToList(); string result = string.Empty; foreach (PropertyInfo pi in propertyInfos) { LoggableRelationshipAttribute display = pi.GetCustomAttribute(typeof(LoggableRelationshipAttribute)) as LoggableRelationshipAttribute; object value = type.GetProperty(pi.Name).GetValue(obj, null); if (value == null) { continue; } Type relationshipType = value.GetType(); object relationshipValue = relationshipType.GetProperty(display.PropertyName).GetValue(value, null); object relationshipTypeName = display.PropertyType ?? relationshipType.GetProperty("Type").GetValue(value, null); result += $" {display.Preposition} {relationshipTypeName}: '{relationshipValue}', "; } if (!string.IsNullOrEmpty(result)) { result = result.Remove(result.Length - 2).Insert(result.Length - 2, "."); } return(result); }
private static string AutoLogRelationshipUpdate <T>(T obj, Type type, object oldObj) { if (obj == null) { throw new ArgumentNullException("Parametro 'obj' não pode ser nulo"); } if (type == null) { throw new ArgumentNullException("Parametro 'type' não pode ser nulo"); } if (oldObj == null) { throw new ArgumentNullException("Parametro 'OldValue' não pode ser nulo"); } List <PropertyInfo> propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where( prop => Attribute.IsDefined(prop, typeof(LoggableRelationshipAttribute))).ToList(); string result = string.Empty; foreach (PropertyInfo pi in propertyInfos) { LoggableRelationshipAttribute display = pi.GetCustomAttribute(typeof(LoggableRelationshipAttribute)) as LoggableRelationshipAttribute; object value = type.GetProperty(pi.Name).GetValue(obj, null); object valueId = type.GetProperty(pi.Name + "Id").GetValue(obj, null); object oldValue = type.GetProperty(pi.Name).GetValue(oldObj, null); object oldValueId = type.GetProperty(pi.Name + "Id").GetValue(oldObj, null); if (value == null) { continue; } if (valueId.Equals(oldValueId)) { continue; } Type relationshipType = value.GetType(); PropertyInfo relationshipPropInfo = relationshipType.GetProperty(display.PropertyName); if (relationshipPropInfo == null) { throw new NullReferenceException($"Não existe propriedade '{display.PropertyName}' na classe {relationshipType.Name}"); } object relationshipTypeName = display.PropertyType ?? relationshipType.GetProperty("Type").GetValue(value, null); object relationshipValue = value == null ? "Nenhum" : relationshipPropInfo.GetValue(value, null); object relationshipOldValue = oldValue == null ? "Nenhum" : relationshipPropInfo.GetValue(oldValue, null); result += $" {display.PrepositionOnUpdate ?? display.Preposition} {relationshipTypeName} de '{GetTextFromType(relationshipPropInfo, relationshipOldValue)}' para '{GetTextFromType(relationshipPropInfo, relationshipValue)}', "; } if (!string.IsNullOrEmpty(result)) { result = result.Remove(result.Length - 2).Insert(result.Length - 2, "."); } return(result); }