Esempio n. 1
0
        /// <summary>
        /// Emits MetaHandle that contains type handle for reference handle only when this referenced is added to pool for the first time.
        /// Emits inlined string for strings and inlined value types for boxed objects.
        /// Emits additional array dimensions info for array refernces who's types are emitted for the first time
        /// </summary>
        public MetaHandle GetHandle(object reference, TypeRegistry treg, SlimFormat format, out Type type)
        {
            Debug.Assert(m_Mode == SerializationOperation.Serializing, "GetHandle() called while deserializing", DebugAction.Throw);

            if (reference == null)
            {
                type = null;
                return(new MetaHandle(0));
            }

            type = reference.GetType();

            if (type == typeof(string))
            {
                return(MetaHandle.InlineString(reference as string));
            }

            if (reference is Type)
            {
                var thandle = treg.GetTypeHandle(reference as Type);
                return(MetaHandle.InlineTypeValue(thandle));
            }


            if (type.IsValueType)
            {
                var vth = treg.GetTypeHandle(type);
                return(MetaHandle.InlineValueType(vth));
            }

            bool added;

            uint handle = (uint)getIndex(reference, out added);

            if (added)
            {
                var th = treg.GetTypeHandle(type);

                if (format.IsRefTypeSupported(type))//20150305 Refhandle inline
                {
                    return(MetaHandle.InlineRefType(th));
                }

                if (type.IsArray)//write array header like so:  "System.int[,]|0~10,0~12" or "$3|0~10,0~12"
                {
                    //DKh 20130712 Removed repetitive code that was refactored into Arrays class
                    var arr = (Array)reference;
                    th = new VarIntStr(Arrays.ArrayToDescriptor(arr, type, th));
                }

                return(new MetaHandle(handle, th));
            }
            return(new MetaHandle(handle));
        }
Esempio n. 2
0
        private void serializeInfo(SlimWriter writer, TypeRegistry registry, RefPool refs, SerializationInfo info, StreamingContext streamingContext)
        {
            writer.Write(registry.GetTypeHandle(info.ObjectType));//20171223 DKh
            writer.Write(info.MemberCount);

            var senum = info.GetEnumerator();

            while (senum.MoveNext())
            {
                writer.Write(senum.Name);
                writer.Write(registry.GetTypeHandle(senum.ObjectType));
                Schema.Serialize(writer, registry, refs, senum.Value, streamingContext);
            }
        }
Esempio n. 3
0
        public void Serialize(SlimWriter writer, TypeRegistry registry, RefPool refs, object instance, StreamingContext streamingContext, Type valueType = null)
        {
            Type type = valueType;

            VarIntStr typeHandle = new VarIntStr(0);

            if (type == null)
            {
                if (instance == null)
                {
                    writer.Write(TypeRegistry.NULL_HANDLE);//object type null
                    return;
                }

                type = instance.GetType();

                //Write type name. Full or compressed. Full Type names are assembly-qualified strings, compressed are string in form of
                // $<name_table_index> i.e.  $1 <--- get string[1]
                typeHandle = registry.GetTypeHandle(type);
                writer.Write(typeHandle);
            }

            //we get here if we have a boxed value of directly-handled type
            var wa = Format.GetWriteActionForType(type) ?? Format.GetWriteActionForRefType(type);//20150503 DKh fixed root byte[] slow

            if (wa != null)
            {
                wa(writer, instance);
                return;
            }

            TypeDescriptor td = getTypeDescriptorCachedOrMake(type);

            if (td.IsArray) //need to write array dimensions
            {
                writer.Write(Arrays.ArrayToDescriptor((Array)instance, type, typeHandle));
            }

            td.SerializeInstance(writer, registry, refs, instance, streamingContext);
        }