Пример #1
0
        protected override void CreateOrTransformObject(ref ObjectContext objectContext)
        {
            if (recursionLevel >= recursionMaxExpectedDepth)
            {
                // Create appropriate reference type for both serialization and deserialization
                if (objectContext.SerializerContext.IsSerializing)
                {
                    var entityComponent = objectContext.Instance as EntityComponent;
                    var entityScript    = objectContext.Instance as Script;
                    if (entityComponent != null)
                    {
                        objectContext.Instance = new EntityComponentReference(entityComponent);
                    }
                    else if (entityScript != null && levelSinceScriptComponent != 1)
                    {
                        var script = new EntityScriptReference(entityScript);
                        objectContext.Instance = script;
                        objectContext.Tag      = objectContext.Settings.TagTypeRegistry.TagFromType(entityScript.GetType());
                    }
                    else if (objectContext.Instance is Entity)
                    {
                        objectContext.Instance = new EntityReference {
                            Id = ((Entity)objectContext.Instance).Id
                        };
                    }
                }
                else
                {
                    var type = objectContext.Descriptor.Type;
                    if (typeof(EntityComponent).IsAssignableFrom(type))
                    {
                        objectContext.Instance = new EntityComponentReference();
                    }
                    else if (typeof(Script).IsAssignableFrom(type) && levelSinceScriptComponent != 1)
                    {
                        objectContext.Instance = new EntityScriptReference {
                            ScriptType = objectContext.Descriptor.Type
                        };
                    }
                    else if (type == typeof(Entity))
                    {
                        objectContext.Instance = new EntityReference();
                    }
                }
            }

            base.CreateOrTransformObject(ref objectContext);
        }
        protected override void CreateOrTransformObject(ref ObjectContext objectContext)
        {
            if (isSerializingAsReference)
            {
                // Create appropriate reference type for both serialization and deserialization
                if (objectContext.SerializerContext.IsSerializing)
                {
                    var entityComponent = objectContext.Instance as EntityComponent;
                    var entityScript = objectContext.Instance as Script;
                    if (entityComponent != null)
                    {
                        objectContext.Instance = new EntityComponentReference(entityComponent);
                    }
                    else if (entityScript != null && scriptLevel > 1)
                    {
                        var script = new EntityScriptReference(entityScript);
                        objectContext.Instance = script;
                        objectContext.Tag = objectContext.Settings.TagTypeRegistry.TagFromType(entityScript.GetType());
                    }
                    else if (objectContext.Instance is Entity)
                    {
                        objectContext.Instance = new EntityReference { Id = ((Entity)objectContext.Instance).Id };
                    }
                }
                else
                {
                    var type = objectContext.Descriptor.Type;
                    if (typeof(EntityComponent).IsAssignableFrom(type))
                    {
                        objectContext.Instance = new EntityComponentReference();
                    }
                    else if (typeof(Script).IsAssignableFrom(type) && scriptLevel > 1)
                    {
                        objectContext.Instance = new EntityScriptReference { ScriptType = objectContext.Descriptor.Type };
                    }
                    else if (type == typeof(Entity))
                    {
                        objectContext.Instance = new EntityReference();
                    }
                }
            }

            base.CreateOrTransformObject(ref objectContext);
        }