/**
         * <summary>Read dictionary.</summary>
         */
        private static object ReadDictionary(PortableReaderImpl ctx, Type type)
        {
            PortableCollectionInfo info = PortableCollectionInfo.Info(type);

            return(info.IsGenericDictionary
                ? info.ReadGeneric(ctx)
                : PortableUtils.ReadDictionary(ctx, null));
        }
        /**
         * <summary>Write generic dictionary.</summary>
         */
        private static void WriteGenericDictionary(PortableWriterImpl ctx, object obj)
        {
            PortableCollectionInfo info = PortableCollectionInfo.Info(obj.GetType());

            Debug.Assert(info.IsGenericDictionary, "Not generic dictionary: " + obj.GetType().FullName);

            ctx.Stream.WriteByte(PortableUtils.TypeDictionary);

            info.WriteGeneric(ctx, obj);
        }
Example #3
0
        /// <summary>
        /// Get's metadata field type ID for the given type.
        /// </summary>
        /// <param name="type">Type.</param>
        /// <returns>Type ID.</returns>
        private static int TypeId(Type type)
        {
            int typeId;

            if (TypeIds.TryGetValue(type, out typeId))
            {
                return(typeId);
            }
            if (type.IsEnum)
            {
                return(PortableUtils.TypeEnum);
            }
            if (type.IsArray)
            {
                return(type.GetElementType().IsEnum ? PortableUtils.TypeArrayEnum : PortableUtils.TypeArray);
            }
            PortableCollectionInfo colInfo = PortableCollectionInfo.Info(type);

            return(colInfo.IsAny ? colInfo.IsCollection || colInfo.IsGenericCollection ?
                   PortableUtils.TypeCollection : PortableUtils.TypeDictionary : PortableUtils.TypeObject);
        }
        /**
         * <summary>Get write handler for type.</summary>
         * <param name="type">Type.</param>
         * <returns>Handler or null if cannot be hanled in special way.</returns>
         */
        public static PortableSystemWriteDelegate WriteHandler(Type type)
        {
            PortableSystemWriteDelegate handler;

            if (WriteHandlers.TryGetValue(type, out handler))
            {
                return(handler);
            }

            // 1. Array?
            if (type.IsArray)
            {
                if (type.GetElementType().IsEnum)
                {
                    return(WriteEnumArray);
                }
                return(WriteArray);
            }

            // 2. Enum?
            if (type.IsEnum)
            {
                return(WriteEnum);
            }

            // 3. Collection?
            PortableCollectionInfo info = PortableCollectionInfo.Info(type);

            if (info.IsAny)
            {
                return(info.WriteHandler);
            }

            // No special handler found.
            return(null);
        }