Beispiel #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));
        }
Beispiel #2
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)
        {
            if (reference == null)
            {
                type = null;
                return(new MetaHandle(0));
            }

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

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

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

            bool added;

            uint   handle = (uint)getIndex(reference, out added);
            string th     = added ? treg.GetTypeHandle(type) : null;

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

                if (reference is Array)//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 = Arrays.ArrayToDescriptor(arr, treg, type, th);
                }
            }
            return(new MetaHandle(handle, th));
        }