示例#1
0
        public ContentTypeSerializer GetTypeSerializer(Type type)
        {
            // Create the known serializers if we haven't already.
            if (_serializers == null)
            {
                _serializers = new Dictionary <Type, ContentTypeSerializer>();

                var types = ContentTypeSerializerAttribute.GetTypes();
                foreach (var t in types)
                {
                    if (!t.IsGenericType)
                    {
                        var cts = Activator.CreateInstance(t) as ContentTypeSerializer;
                        cts.Initialize(this);
                        _serializers.Add(cts.TargetType, cts);
                    }
                }
            }

            // Look it up.
            ContentTypeSerializer serializer;

            if (_serializers.TryGetValue(type, out serializer))
            {
                return(serializer);
            }

            // If we still don't have a serializer then
            // fallback to the reflection based serializer.
            if (serializer == null)
            {
                serializer = new ReflectiveSerializer(type);
                serializer.Initialize(this);
            }

            _serializers.Add(type, serializer);
            return(serializer);
        }
示例#2
0
        public ContentTypeSerializer GetTypeSerializer(Type type)
        {
            // Create the known serializers if we haven't already.
            if (_serializers == null)
            {
                _serializers            = new Dictionary <Type, ContentTypeSerializer>();
                _genericSerializerTypes = new Dictionary <Type, Type>();

                var types = ContentTypeSerializerAttribute.GetTypes();
                foreach (var t in types)
                {
                    if (t.IsGenericType)
                    {
                        var genericType = t.BaseType.GetGenericArguments()[0];
                        _genericSerializerTypes.Add(genericType.GetGenericTypeDefinition(), t);
                    }
                    else
                    {
                        var cts = Activator.CreateInstance(t) as ContentTypeSerializer;
                        cts.Initialize(this);
                        _serializers.Add(cts.TargetType, cts);
                    }
                }
            }

            // Look it up.
            ContentTypeSerializer serializer;

            if (_serializers.TryGetValue(type, out serializer))
            {
                return(serializer);
            }

            Type serializerType;

            if (type.IsArray)
            {
                if (type.GetArrayRank() != 1)
                {
                    throw new RankException("We only support single dimension arrays.");
                }

                var arrayType = typeof(ArraySerializer <>).MakeGenericType(new[] { type.GetElementType() });
                serializer = (ContentTypeSerializer)Activator.CreateInstance(arrayType);
            }
            else if (type.IsGenericType && _genericSerializerTypes.TryGetValue(type.GetGenericTypeDefinition(), out serializerType))
            {
                serializerType = serializerType.MakeGenericType(type.GetGenericArguments());
                serializer     = (ContentTypeSerializer)Activator.CreateInstance(serializerType);
            }
            else if (type.IsEnum)
            {
                serializer = new EnumSerializer(type);
            }
            else if (typeof(IList).IsAssignableFrom(type))
            {
                // Special handling for non-generic IList types. By the time we get here,
                // generic collection types will already have been handled by one of the known serializers.
                serializer = new NonGenericIListSerializer(type);
            }
            else
            {
                // The reflective serializer is not for primitive types!
                if (type.IsPrimitive)
                {
                    throw new NotImplementedException(string.Format("Unhandled primitive type `{0}`!", type.FullName));
                }

                // We still don't have a serializer then we
                // fallback to the reflection based serializer.
                serializer = new ReflectiveSerializer(type);
            }

            Debug.Assert(serializer.TargetType == type, "Target type mismatch!");

            // We cache the serializer before we initialize it to
            // avoid a stack overflow on recursive types.
            _serializers.Add(type, serializer);
            serializer.Initialize(this);

            return(serializer);
        }