Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/01/10 12:37:16 (15 years ago)
Author:
epitzer
Message:

throw Exception if class is marked [Storable] but cannot be serialized with the StorableSerializer (#548)

Location:
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableReflection.cs

    r3554 r3577  
    144144
    145145    private static StorableClassAttribute GetStorableClassAttribute(Type type) {
    146       return (StorableClassAttribute)type
    147         .GetCustomAttributes(typeof(StorableClassAttribute), false)
    148         .SingleOrDefault();
     146      lock (storableClassCache) {
     147        if (storableClassCache.ContainsKey(type))
     148          return storableClassCache[type];
     149        StorableClassAttribute attribute = type
     150          .GetCustomAttributes(typeof(StorableClassAttribute), false)
     151          .SingleOrDefault() as StorableClassAttribute;
     152        storableClassCache.Add(type, attribute);
     153        return attribute;
     154      }
    149155    }
    150156
    151     private static bool HasStorableClassAttribute(Type type) {
    152       return type.GetCustomAttributes(typeof(StorableClassAttribute), false).Length > 0;
     157    public static bool HasStorableClassAttribute(Type type) {
     158      return GetStorableClassAttribute(type) != null;
    153159    }
     160
     161    private static Dictionary<Type, StorableClassAttribute> storableClassCache =
     162      new Dictionary<Type, StorableClassAttribute>();
    154163
    155164    #endregion
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs

    r3553 r3577  
    3939    /// </returns>
    4040    public bool CanSerialize(Type type) {
     41      bool markedStorable = StorableReflection.HasStorableClassAttribute(type);
    4142      if (GetConstructor(type) == null)
    42         return false;
    43       return StorableReflection.IsEmptyOrStorableType(type, true);
     43        if (markedStorable)
     44          throw new Exception("[Storable] type has no default constructor and no [StorableConstructor]");
     45        else
     46          return false;
     47      if (!StorableReflection.IsEmptyOrStorableType(type, true))
     48        if (markedStorable)
     49          throw new Exception("[Storable] type has non emtpy, non [Storable] base classes");
     50        else
     51          return false;
     52      return true;
    4453    }
    4554
     
    5362    /// </returns>
    5463    public string JustifyRejection(Type type) {
     64      StringBuilder sb = new StringBuilder();
    5565      if (GetConstructor(type) == null)
    56         return "no default constructor and no storable constructor";
     66        sb.Append("class has no default constructor and no [StorableConstructor]");
    5767      if (!StorableReflection.IsEmptyOrStorableType(type, true))
    58         return "class is not marked with the storable class attribute";
    59       return "no reason";
     68        sb.Append("class (or one of its bases) is not empty and not marked [Storable]; ");     
     69      return sb.ToString();
    6070    }
    6171
     
    154164
    155165    private Dictionary<HookDesignator, List<StorableReflection.Hook>> hookCache =
    156       new Dictionary<HookDesignator, List<StorableReflection.Hook>>();
     166      new Dictionary<HookDesignator, List<StorableReflection.Hook>>();   
    157167
    158168    #endregion
Note: See TracChangeset for help on using the changeset viewer.