Esempio n. 1
0
        /// <summary>
        /// Determines for every type whether the composite serializer is applicable.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>
        ///     <c>true</c> if this instance can serialize the specified type; otherwise, <c>false</c>.
        /// </returns>
        public bool CanSerialize(Type type)
        {
            var markedStorable = StorableReflection.HasStorableClassAttribute(type);

            if (GetConstructor(type) == null)
            {
                if (markedStorable)
                {
                    throw new Exception("[Storable] type has no default constructor and no [StorableConstructor]");
                }
                else
                {
                    return(false);
                }
            }
            if (!StorableReflection.IsEmptyOrStorableType(type, true))
            {
                if (markedStorable)
                {
                    throw new Exception("[Storable] type has non emtpy, non [Storable] base classes");
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Determines for every type whether the composite serializer is applicable.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>
        ///     <c>true</c> if this instance can serialize the specified type; otherwise, <c>false</c>.
        /// </returns>
        public bool CanSerialize(Type type)
        {
            if (type.IsEnum || type.IsValueType)
            {
                return(false);                           // there are other more specific serializers for enums and structs
            }
            var markedStorable = StorableReflection.HasStorableTypeAttribute(type);

            if (GetConstructor(type) == null)
            {
                if (markedStorable && !type.IsInterface)
                {
                    throw new Exception("[Storable] type has no default constructor and no [StorableConstructor]");
                }
                else
                {
                    return(false);
                }
            }
            if (!StorableReflection.IsEmptyOrStorableType(type, true))
            {
                if (markedStorable && !type.IsInterface)
                {
                    throw new Exception("[Storable] type has non emtpy, non [Storable] base classes");
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Give a reason if possibly why the given type cannot be serialized by this
        /// ICompositeSerializer.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>
        /// A string justifying why type cannot be serialized.
        /// </returns>
        public string JustifyRejection(Type type)
        {
            var sb = new StringBuilder();

            if (GetConstructor(type) == null)
            {
                sb.Append("class has no default constructor and no [StorableConstructor]");
            }
            if (!StorableReflection.IsEmptyOrStorableType(type, true))
            {
                sb.Append("class (or one of its bases) is not empty and not marked [Storable]; ");
            }
            return(sb.ToString());
        }