Пример #1
0
        /// <summary>
        /// Gets the serializer from array or inherit.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">The object.</param>
        /// <param name="type">The type.</param>
        /// <param name="serializerConverter">The serializer converter.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        /// <exception cref="InvalidOperationException">Invalid array subtype</exception>
        private static bool GetSerializerFromArrayOrInherit <T>(T obj, ref Type type,
                                                                out SerializerConverter <T> serializerConverter) where T : class
        {
            serializerConverter = null;

            if (type.IsArray)
            {
                return(GetSerializerFromArray(obj, ref type, ref serializerConverter));
            }

            return(typeof(IEnumerable).IsAssignableFrom(type) && GetSerializerFromInherit(obj, ref type, ref serializerConverter));
        }
Пример #2
0
        /// <summary>
        /// Gets the serializer from inherit.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">The object.</param>
        /// <param name="type">The type.</param>
        /// <param name="serializerConverter">The serializer converter.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        private static bool GetSerializerFromInherit <T>(T obj, ref Type type, ref SerializerConverter <T> serializerConverter)
            where T : class
        {
            type = type.GetGenericArguments()[0];

            var attribute = Attribute.GetCustomAttribute(type, typeof(SerializerAttribute)) as SerializerAttribute;

            if (attribute == null)
            {
                return(false);
            }

            serializerConverter = GetSerializer(obj, attribute);

            return(true);
        }
Пример #3
0
        /// <summary>
        /// Gets the serializer from array.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">The object.</param>
        /// <param name="type">The type.</param>
        /// <param name="serializerConverter">The serializer converter.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        /// <exception cref="InvalidOperationException">Invalid array subtype</exception>
        private static bool GetSerializerFromArray <T>(T obj, ref Type type, ref SerializerConverter <T> serializerConverter)
            where T : class
        {
            type = type.GetElementType();

            var attribute = Attribute.GetCustomAttribute(type ?? throw new InvalidOperationException("Invalid array subtype"),
                                                         typeof(SerializerAttribute)) as SerializerAttribute;

            if (attribute == null)
            {
                return(false);
            }

            serializerConverter = GetSerializer(obj, attribute);

            return(true);
        }