private void addProperties(ModelMap map, ModelMap overrides, ModelMapDiffOptions options) { var targetIndex = -1; var instructionsToAdd = new List <IModelMapInstruction>(); var contexts = new Stack <IModelMapInstruction>(); foreach (var instruction in overrides.Instructions.Where(_ => _.GetType() != typeof(Instructions.RemoveProperty)).ToArray()) { var mapInstructions = map.Instructions.ToList(); if (shouldOffset(instruction, options)) { var i = mapInstructions.IndexOf(instruction); if (i != -1) { targetIndex = i + 1; } } if (targetIndex == -1) { continue; } var context = options.PropertyContexts.SingleOrDefault(_ => _.Matches(instruction.GetType())); if (context != null && mapInstructions.IndexOf(instruction) == -1) { contexts.Push(context.WaitFor()); } if (contexts.Count != 0) { instructionsToAdd.Add(instruction); } if (contexts.Count != 0 && contexts.Peek().GetType() == instruction.GetType()) { contexts.Pop(); } if (contexts.Count == 0 && instructionsToAdd.Count != 0) { map.InsertInstructions(targetIndex, instructionsToAdd); targetIndex += instructionsToAdd.Count; instructionsToAdd.Clear(); } } }