/// <summary>
        /// Initialize the writer.
        /// </summary>
        /// <param name="compiler">Compiler instance calling this writer.</param>
        protected override void Initialize(ContentCompiler compiler)
        {
            base.Initialize(compiler);

            keyWriter   = compiler.GetTypeWriter(typeof(K));
            valueWriter = compiler.GetTypeWriter(typeof(V));
        }
예제 #2
0
        /// <summary>
        /// Gets a ContentTypeWriter for the given type.
        /// </summary>
        /// <param name="type">The type of the object to write.</param>
        /// <returns>The ContentTypeWriter for the type.</returns>
        internal ContentTypeWriter GetTypeWriter(Type type)
        {
            ContentTypeWriter typeWriter = null;

            if (!typeMap.TryGetValue(type, out typeWriter))
            {
                int index = typeWriters.Count;
                typeWriter = compiler.GetTypeWriter(type);

                typeWriters.Add(typeWriter);
                typeWriterMap.Add(typeWriter.GetType(), index);
                typeMap.Add(type, typeWriter);

                // TODO: This is kinda messy.. seems like there could
                // be a better way for generics and arrays to register
                // their inner types with the typeWriterMap.
                if (type.IsGenericType)
                {
                    var args = type.GetGenericArguments();
                    foreach (var arg in args)
                    {
                        GetTypeWriter(arg);
                    }
                }
                else if (type.IsArray)
                {
                    GetTypeWriter(type.GetElementType());
                }
            }
            return(typeWriter);
        }
예제 #3
0
        /// <summary>
        /// Initialize the writer.
        /// </summary>
        /// <param name="compiler">Compiler instance calling this writer.</param>
        protected override void Initialize(ContentCompiler compiler)
        {
            base.Initialize(compiler);

            underlyingType = Enum.GetUnderlyingType(typeof(T));
            elementWriter  = compiler.GetTypeWriter(underlyingType);
        }
예제 #4
0
        protected override void Initialize(ContentCompiler compiler)
        {
            var type = ReflectionHelpers.GetBaseType(TargetType);

            if (type != null && type != typeof(object))
            {
                _baseType       = type;
                _baseTypeWriter = compiler.GetTypeWriter(_baseType);
            }

            var runtimeType = TargetType.GetCustomAttributes(typeof(ContentSerializerRuntimeTypeAttribute), false).FirstOrDefault() as ContentSerializerRuntimeTypeAttribute;

            if (runtimeType != null)
            {
                _runtimeType = runtimeType.RuntimeType;
            }

            var typeVersion = TargetType.GetCustomAttributes(typeof(ContentSerializerTypeVersionAttribute), false).FirstOrDefault() as ContentSerializerTypeVersionAttribute;

            if (typeVersion != null)
            {
                _typeVersion = typeVersion.TypeVersion;
            }

            _properties = TargetType.GetAllProperties();
            _fields     = TargetType.GetAllFields();
        }
예제 #5
0
        /// <summary>
        /// Gets a ContentTypeWriter for the given type.
        /// </summary>
        /// <param name="type">The type of the object to write.</param>
        /// <returns>The ContentTypeWriter for the type.</returns>
        internal ContentTypeWriter GetTypeWriter(Type type)
        {
            ContentTypeWriter typeWriter = null;

            if (!typeMap.TryGetValue(type, out typeWriter))
            {
                int index = typeWriters.Count;
                typeWriter = compiler.GetTypeWriter(type);

                typeWriters.Add(typeWriter);
                typeWriterMap.Add(typeWriter.GetType(), index);
                typeMap.Add(type, typeWriter);

                typeWriter.OnAddedToContentWriter(this);
            }
            return(typeWriter);
        }
예제 #6
0
        private bool IsValidProperty(PropertyInfo property)
        {
            // Properties must have at least a getter.
            if (property.CanRead == false)
            {
                return(false);
            }

            // Skip over indexer properties.
            if (property.Name == "Item")
            {
                var getMethod = ReflectionHelpers.GetPropertyGetMethod(property);
                var setMethod = ReflectionHelpers.GetPropertySetMethod(property);

                if ((getMethod != null && getMethod.GetParameters().Length > 0) ||
                    (setMethod != null && setMethod.GetParameters().Length > 0))
                {
                    return(false);
                }
            }

            // Are we explicitly asked to ignore this item?
            if (ReflectionHelpers.GetCustomAttribute <ContentSerializerIgnoreAttribute>(property) != null)
            {
                return(false);
            }

            var contentSerializerAttribute = ReflectionHelpers.GetCustomAttribute <ContentSerializerAttribute>(property);

            if (contentSerializerAttribute == null)
            {
                // There is no ContentSerializerAttribute, so non-public
                // properties cannot be serialized.
                if (!ReflectionHelpers.PropertyIsPublic(property))
                {
                    return(false);
                }

                // Check the type reader to see if it is safe to
                // deserialize into the existing type.
                if (!property.CanWrite)
                {
                    if (!_compiler.GetTypeWriter(property.PropertyType).CanDeserializeIntoExistingObject)
                    {
                        return(false);
                    }
                }
            }
            else if (contentSerializerAttribute.SharedResource)
            {
                _sharedResources.Add(property);
            }

            return(true);
        }
예제 #7
0
        protected override void Initialize(ContentCompiler compiler)
        {
            base.Initialize(compiler);

            if (TargetType.IsGenericType)
            {
                _genericTypes = new List <ContentTypeWriter>();
                var arguments = TargetType.GetGenericArguments();
                foreach (var arg in arguments)
                {
                    _genericTypes.Add(compiler.GetTypeWriter(arg));
                }
            }
        }
예제 #8
0
        /// <summary>
        /// Gets a ContentTypeWriter for the given type.
        /// </summary>
        /// <param name="type">The type of the object to write.</param>
        /// <returns>The ContentTypeWriter for the type.</returns>
        internal ContentTypeWriter GetTypeWriter(Type type)
        {
            ContentTypeWriter typeWriter = null;

            if (!typeMap.TryGetValue(type, out typeWriter))
            {
                int index = typeWriters.Count;
                typeWriter = compiler.GetTypeWriter(type);
                typeWriters.Add(typeWriter);
                typeWriterMap.Add(typeWriter, index);
                typeMap.Add(type, typeWriter);

                var args = type.GetGenericArguments();
                foreach (var arg in args)
                {
                    GetTypeWriter(arg);
                }
            }
            return(typeWriter);
        }
예제 #9
0
        /// <summary>
        /// Initialize the writer.
        /// </summary>
        /// <param name="compiler">Compiler instance calling this writer.</param>
        protected override void Initialize(ContentCompiler compiler)
        {
            base.Initialize(compiler);

            elementWriter = compiler.GetTypeWriter(typeof(T));
        }