public InstanceSave GetInstance(IPositionedSizedObject representation, ElementSave instanceContainer, string prefix, InstanceFetchType fetchType, List <ElementWithState> elementStack)
        {
            if (instanceContainer == null)
            {
                return(null);
            }

            InstanceSave toReturn = null;


            string qualifiedName = representation.GetAttachmentQualifiedName(elementStack);

            // strip off the guide name if it starts with a guide
            qualifiedName = StripGuideOrParentNameIfNecessaryName(qualifiedName, representation);


            foreach (InstanceSave instanceSave in instanceContainer.Instances)
            {
                if (prefix + instanceSave.Name == qualifiedName)
                {
                    toReturn = instanceSave;
                    break;
                }
            }

            if (toReturn == null)
            {
                foreach (InstanceSave instanceSave in instanceContainer.Instances)
                {
                    ElementSave instanceElement = instanceSave.GetBaseElementSave();

                    toReturn = GetInstance(representation, instanceElement, prefix + instanceSave.Name + ".", fetchType, elementStack);

                    if (toReturn != null)
                    {
                        if (fetchType == InstanceFetchType.DeepInstance)
                        {
                            // toReturn will be toReturn, no need to do anything
                        }
                        else // fetchType == InstanceInCurrentElement
                        {
                            toReturn = instanceSave;
                        }
                        break;
                    }
                }
            }

            return(toReturn);
        }
        public static string GetAttachmentQualifiedName(this IPositionedSizedObject ipso, List <ElementWithState> elementStack)
        {
            IPositionedSizedObject parent = ipso.Parent;
            IPositionedSizedObject child  = ipso;

            while (parent != null)
            {
                if (parent.Tag is ElementSave || parent.Tag == null)
                {
                    // we found it, so break!
                    break;
                }
                else
                {
                    InstanceSave thisInstance = child.Tag as InstanceSave;

                    if (thisInstance.IsParentASibling(elementStack))
                    {
                        child  = parent;
                        parent = parent.Parent;
                    }
                    else
                    {
                        // we found it, so break;
                        break;
                    }
                }
            }


            if (parent == null)
            {
                return(ipso.Name);
            }
            else
            {
                var parentName = parent.GetAttachmentQualifiedName(elementStack);
                if (!string.IsNullOrEmpty(parentName))
                {
                    return(parentName + "." + ipso.Name);
                }
                else
                {
                    return(ipso.Name);
                }
            }
        }