Exemplo 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);
        }
Exemplo 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);
        }
Exemplo 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());
        }
Exemplo n.º 4
0
 private IEnumerable <StorableReflection.Hook> GetHooks(HookType hookType, Type type)
 {
     lock (hookCache) {
         List <StorableReflection.Hook> hooks;
         var designator = new HookDesignator(type, hookType);
         hookCache.TryGetValue(designator, out hooks);
         if (hooks != null)
         {
             return(hooks);
         }
         hooks = new List <StorableReflection.Hook>(StorableReflection.CollectHooks(hookType, type));
         hookCache.Add(designator, hooks);
         return(hooks);
     }
 }
Exemplo n.º 5
0
 private IEnumerable <DataMemberAccessor> GetStorableAccessors(Type type)
 {
     lock (accessorListCache) {
         if (accessorListCache.ContainsKey(type))
         {
             return(accessorListCache[type]);
         }
         var storableMembers = StorableReflection
                               .GenerateStorableMembers(type)
                               .Select(GetMemberAccessor)
                               .ToList();
         accessorListCache[type] = storableMembers;
         return(storableMembers);
     }
 }