GetTypeInfo() public static method

public static GetTypeInfo ( Type type ) : TypeInfo
type System.Type
return TypeInfo
Ejemplo n.º 1
0
        object DeserializeArray(Type expectedType)
        {
            Trace.Assert(m_reader.TokenType == JsonToken.StartArray);

            Read();

            var list = new List <object>();

            Type     elementType = null;
            TypeInfo typeInfo    = null;

            if (expectedType != null)
            {
                typeInfo    = TypeInfo.GetTypeInfo(expectedType);
                elementType = typeInfo.ElementType1;
            }

            while (m_reader.TokenType != JsonToken.EndArray)
            {
                var ob = DeserializeObject(elementType);
                list.Add(ob);
                Read();
            }

            if (expectedType == null || expectedType == typeof(object))
            {
                return(list);
            }

            switch (typeInfo.TypeClass)
            {
            case TypeClass.Array:
            {
                var arr = Array.CreateInstance(expectedType.GetElementType(), list.Count);
                Array.Copy(list.ToArray(), arr, list.Count);
                return(arr);
            }

            case TypeClass.GenericList:
            {
                var ilist = (IList)Activator.CreateInstance(expectedType);
                foreach (var ob in list)
                {
                    ilist.Add(ob);
                }
                return(ilist);
            }

            default:
                throw new Exception();
            }
        }
Ejemplo n.º 2
0
        void WriteGenericDictionary(object ob, TypeInfo typeInfo, bool writeType)
        {
            m_writer.WriteStartObject();

            if (writeType)
            {
                m_writer.WritePropertyName("$type");
                m_writer.WriteValue(typeInfo.Type.FullName);
            }

            var dict = (IDictionary)ob;

            var keyType   = typeInfo.ElementType1;
            var valueType = typeInfo.ElementType2;

            var enumerator = dict.GetEnumerator();

            var keyTypeInfo = TypeInfo.GetTypeInfo(keyType);

            if (keyTypeInfo.TypeConverter == null)
            {
                throw new Exception();
            }

            while (enumerator.MoveNext())
            {
                var key   = enumerator.Key;
                var value = enumerator.Value;

                var keyStr = keyTypeInfo.TypeConverter.ConvertToInvariantString(key);

                m_writer.WritePropertyName(keyStr);
                SerializeObject(value, valueType);
            }

            m_writer.WriteEndObject();
        }
Ejemplo n.º 3
0
        object DeserializeFullObject(Type expectedType)
        {
            Trace.Assert(m_reader.TokenType == JsonToken.StartObject);

            Read();

            object ob;

            if (m_reader.TokenType == JsonToken.PropertyName && (string)m_reader.Value == "$ref")
            {
                Read();

                Trace.Assert(m_reader.TokenType == JsonToken.Integer);

                var id = (int)(long)m_reader.Value;

                ob = m_referenceResolver.Get(id);

                if (ob == null)
                {
                    throw new Exception();
                }

                Read();

                Trace.Assert(m_reader.TokenType == JsonToken.EndObject);
            }
            else
            {
                var id = ReadID();

                var type = ReadType() ?? expectedType;

                if (type == null)
                {
                    throw new SerializationException("No type for object");
                }

                var typeInfo = TypeInfo.GetTypeInfo(type);

                switch (typeInfo.TypeClass)
                {
                case TypeClass.Dictionary:
                case TypeClass.GenericDictionary:
                    ob = DeserializeDictionary(typeInfo);
                    break;

                case TypeClass.GameObject:
                    ob = DeserializeGameObject(typeInfo, id);
                    break;

                case TypeClass.Serializable:
                    ob = DeserializeSerializable(typeInfo, id);
                    break;

                default:
                    throw new Exception();
                }
            }

            return(ob);
        }
Ejemplo n.º 4
0
        void SerializeObject(object ob, Type containerType)
        {
            if (ob == null)
            {
                m_writer.WriteNull();
                return;
            }

            var type = ob.GetType();

            if (m_globalConverters != null)
            {
                var globalConverter = m_globalConverters.GetGlobalConverter(type);
                if (globalConverter != null)
                {
                    ob = globalConverter.ConvertToSerializable(ob);
                    SerializeObject(ob, containerType);
                    return;
                }
            }

            bool writeType = !type.IsValueType && type != containerType;

            var typeInfo = TypeInfo.GetTypeInfo(type);

            switch (typeInfo.TypeClass)
            {
            case TypeClass.Undefined:
                throw new Exception();

            case TypeClass.Basic:
                m_writer.WriteValue(ob);
                break;

            case TypeClass.Enum:
                if (writeType)
                {
                    throw new Exception();
                }

                var value = typeInfo.TypeConverter.ConvertToInvariantString(ob);
                m_writer.WriteValue(value);

                break;

            case TypeClass.Convertable:
                if (writeType)
                {
                    throw new Exception();
                }

                WriteConvertable(ob, typeInfo);
                break;

            case TypeClass.Array:
            case TypeClass.List:
            case TypeClass.GenericList:
                if (writeType)
                {
                    throw new Exception();
                }

                WriteIEnumerable(ob, typeInfo);
                break;

            case TypeClass.GenericDictionary:
                WriteGenericDictionary(ob, typeInfo, writeType);
                break;

            case TypeClass.Dictionary:
                throw new NotImplementedException();

            case TypeClass.GameObject:
                WriteGameObject(ob, typeInfo, writeType);
                break;

            case TypeClass.Serializable:
                WriteSerializable(ob, typeInfo, writeType);
                break;

            default:
                throw new NotImplementedException();
            }
        }