示例#1
0
        public void Serialize(ref byte[] buffer, ref int offset, Type type)
        {
            // Null
            if (type == null)
            {
                SerializerBinary.WriteUInt32Bias(ref buffer, ref offset, Null, Bias);
                return;
            }

            var typeCache = _serializer.InstanceData.TypeCache;

            // Existing
            if (typeCache.TryGetExistingObjectId(type, out int id))
            {
                SerializerBinary.WriteUInt32Bias(ref buffer, ref offset, id, Bias);
                return;
            }


            // Mode: New


            // Is it a composite type that we have to split into its parts? (aka any generic)
            bool isClosed = !type.ContainsGenericParameters;

            if (isClosed && type.IsGenericType)
            {
                SerializerBinary.WriteUInt32Bias(ref buffer, ref offset, NewGeneric, Bias);

                // Split and write

                // Base
                var baseType = type.GetGenericTypeDefinition();
                Serialize(ref buffer, ref offset, baseType);

                // Args
                var genericArgs = type.GetGenericArguments();

                SerializerBinary.WriteByte(ref buffer, ref offset, (byte)(genericArgs.Length));                 // We need count. Ex: Action<T1> and Action<T1, T2> share the name
                for (int i = 0; i < genericArgs.Length; i++)
                {
                    Serialize(ref buffer, ref offset, genericArgs[i]);
                }

                // Register composite type
                typeCache.RegisterObject(type);
            }
            else
            {
                SerializerBinary.WriteUInt32Bias(ref buffer, ref offset, NewSingle, Bias);

                // Open generic, something that can be serialized alone
                var typeName = _typeBinder.GetBaseName(type);

                // Name
                SerializerBinary.WriteString(ref buffer, ref offset, typeName);

                // Register single type
                typeCache.RegisterObject(type);
            }

            // todo: do we put this only in the if or else part? or is it ok here? it should be ok, since we want to embed the schema of every type
            if (_serializer.Config.VersionTolerance == VersionTolerance.AutomaticEmbedded)
            {
                if (!CerasSerializer.FrameworkAssemblies.Contains(type.Assembly))
                {
                    _serializer.WriteSchemaForType(ref buffer, ref offset, type);
                }
            }
        }
示例#2
0
        public void Serialize(ref byte[] buffer, ref int offset, Type type)
        {
            // Null
            if (type == null)
            {
                SerializerBinary.WriteUInt32Bias(ref buffer, ref offset, Null, Bias);
                return;
            }

            var typeCache = _serializer.InstanceData.TypeCache;

            // Existing
            if (typeCache.TryGetExistingObjectId(type, out int id))
            {
                SerializerBinary.WriteUInt32Bias(ref buffer, ref offset, id, Bias);
                return;
            }


            // Mode: New


            // Is it a composite type that we have to split into its parts? (aka any generic)
            bool isClosed = !type.ContainsGenericParameters;

            if (isClosed && type.IsGenericType)
            {
                SerializerBinary.WriteUInt32Bias(ref buffer, ref offset, NewComposite, Bias);

                // Split and write

                // Base
                var baseType = type.GetGenericTypeDefinition();
                Serialize(ref buffer, ref offset, baseType);


                // Args
                var genericArgs = type.GetGenericArguments();

                SerializerBinary.WriteByte(ref buffer, ref offset, (byte)(genericArgs.Length));                 // We need count. Ex: Action<T1> and Action<T1, T2> share the name.
                for (int i = 0; i < genericArgs.Length; i++)
                {
                    Serialize(ref buffer, ref offset, genericArgs[i]);
                }


                // Register composite type
                typeCache.RegisterObject(type);
            }
            else
            {
                SerializerBinary.WriteUInt32Bias(ref buffer, ref offset, NewSingle, Bias);

                // Open generic, something that can be serialized alone

                var typeName = _typeBinder.GetBaseName(type);

                // Name
                SerializerBinary.WriteString(ref buffer, ref offset, typeName);


                // Register single type
                typeCache.RegisterObject(type);
            }
        }