public object Identify(object instance) { var property = _idPropertyFinder.Find(instance.GetType()); if (property == null) { return(instance); } if (property.GetSetMethod(true) == null) { return(instance); } var pval = property.GetValue(instance, null); if (!IsInitialValue(pval)) { return(instance); } var nextId = Interlocked.Increment(ref _nextId); property.SetValue(instance, property.PropertyType == typeof(string) ? (object)nextId.ToString() : Convert.ChangeType(nextId, property.PropertyType), null); return(instance); }
public void Execute(object @object) { if (@object == null) { throw new ArgumentNullException("object"); } var properties = @object.GetType().GetProperties(ReflectionSettings.AllInstance); foreach (var property in properties) { if (!property.PropertyType.IsClass) { continue; } var fkProperty = _foreignKeyFinder.Find(property); if (fkProperty == null) { continue; } var relatedObject = property.GetValue(@object, null); if (relatedObject == null) { continue; } var idProperty = _idPropertyFinder.Find(property.PropertyType); if (idProperty == null) { continue; } if (!fkProperty.PropertyType.IsAssignableFrom(idProperty.PropertyType)) { continue; } var id = idProperty.GetValue(relatedObject, null); fkProperty.SetValue(@object, id, null); } }