public EntityReference SwitchFor(IEntity entity)
        {
            EntityReference reference = this;

            using (LogGroup logGroup = LogGroup.StartDebug("Switching the reference to the perspective of '" + entity.ShortTypeName + "' entity."))
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                LogWriter.Debug("Existing source entity type: " + Type1Name);
                LogWriter.Debug("Existing reference entity type: " + Type2Name);
                LogWriter.Debug("Existing source entity ID: " + Entity1ID.ToString());
                LogWriter.Debug("Existing reference entity ID: " + Entity2ID.ToString());
                LogWriter.Debug("Existing source property name: " + Property1Name.ToString());
                LogWriter.Debug("Existing reference property name: " + Property2Name.ToString());

                SwitchFor(entity.ShortTypeName, entity.ID);
            }

            return(reference);
        }
        public virtual EntityReference SwitchFor(string typeName, Guid id)
        {
            //EntityReference reference = (EntityReference)Clone();
            EntityReference reference = this;

            // TODO: Comment out logging to boost performance
            using (LogGroup logGroup = LogGroup.StartDebug("Switching reference data to the perspective of a '" + typeName + "' entity."))
            {
                if (typeName == null)
                {
                    throw new ArgumentNullException("typeName");
                }

                LogWriter.Debug("Existing source entity type: " + Type1Name);
                LogWriter.Debug("Existing reference entity type: " + Type2Name);
                LogWriter.Debug("Existing source entity ID: " + Entity1ID.ToString());
                LogWriter.Debug("Existing reference entity ID: " + Entity2ID.ToString());
                LogWriter.Debug("Existing source property name: " + Property1Name.ToString());
                LogWriter.Debug("Existing reference property name: " + Property2Name.ToString());

                if (EntitiesUtilities.MatchAlias(typeName, Type1Name) &&
                    Entity1ID == id)
                {
                    LogWriter.Debug("The reference is already in the perspective of the specified entity. No need to switch.");
                }
                else
                {
                    LogWriter.Debug("Switching to the perspective of entity type: " + typeName);

                    Guid entity1ID = Entity1ID;
                    Guid entity2ID = Entity2ID;

                    string type1Name = Type1Name;
                    string type2Name = Type2Name;

                    string property1Name = Property1Name;
                    string property2Name = Property2Name;

                    IEntity originalSourceEntity    = SourceEntity;
                    IEntity originalReferenceEntity = ReferenceEntity;

                    reference.SourceEntity    = originalReferenceEntity;
                    reference.ReferenceEntity = originalSourceEntity;

                    reference.Entity1ID = entity2ID;
                    reference.Entity2ID = entity1ID;

                    reference.Type1Name = type2Name;
                    reference.Type2Name = type1Name;

                    reference.Property1Name = property2Name;
                    reference.Property2Name = property1Name;


                    LogWriter.Debug("New source entity type: " + reference.Type1Name);
                    LogWriter.Debug("New reference entity type: " + reference.Type2Name);
                    LogWriter.Debug("New source entity ID: " + reference.Entity1ID.ToString());
                    LogWriter.Debug("New reference entity ID: " + reference.Entity2ID.ToString());
                    LogWriter.Debug("New source property name: " + reference.Property1Name.ToString());
                    LogWriter.Debug("New reference property name: " + reference.Property2Name.ToString());
                }
            }

            return(reference);
        }