Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/10/10 18:28:50 (15 years ago)
Author:
epitzer
Message:

Make StorableClass attribute compulsory for StorableSerializer to work, add named property StorableClassType to choose between Empty and MarkedOnly, later other options will be added. (#548)

Location:
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable
Files:
1 added
1 deleted
4 edited

Legend:

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

    r2991 r2994  
    77namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable {
    88
     9
     10  /// <summary>
     11  /// Mark the member of a class to be considered by the <code>StorableSerializer</code>.
     12  /// The class must be marked as <code>[StorableClass(StorableClassType.Empty)]</code> and the
     13  /// <code>StorableClassType</code> should be set to <code>MarkedOnly</code> for
     14  /// this attribute to kick in.
     15  /// </summary>
    916  [AttributeUsage(
    1017    AttributeTargets.Field | AttributeTargets.Property,
    1118    AllowMultiple = false,
    12     Inherited = false)]
     19    Inherited = false)]   
    1320  public class StorableAttribute : Attribute {
    1421
     22    /// <summary>
     23    /// An optional name for this member that will be used during serialization.
     24    ///
     25    /// This allows to rename a field/property in code but still be able to read
     26    /// the old serialized format.
     27    /// </summary>
    1528    public string Name { get; set; }
     29
     30
     31    /// <summary>
     32    /// A default value in case the field/property was not present or not serialized
     33    /// in a previous version of the class and could therefore be absent during
     34    /// deserialization.
     35    /// </summary>
    1636    public object DefaultValue { get; set; }
    1737
     
    83103    private static MemberCache memberCache = new MemberCache();
    84104
     105
     106    /// <summary>
     107    /// Get all fields and properties of a class that have the
     108    /// <code>[Storable]</code> attribute set.
     109    /// </summary>   
    85110    public static IEnumerable<StorableMemberInfo> GetStorableMembers(Type type) {
    86111      return GetStorableMembers(type, true);
    87112    }
    88113
     114    /// <summary>
     115    /// Get all fields and properties of a class that have the
     116    /// <code>[Storable]</code> attribute set.
     117    /// </summary>       
     118    /// <param name="inherited">should storable members from base classes be included</param>   
    89119    public static IEnumerable<StorableMemberInfo> GetStorableMembers(Type type, bool inherited) {
    90120      lock (memberCache) {
     
    110140    }
    111141
     142
     143    /// <summary>
     144    /// Get the associated accessors for all storable memebrs.
     145    /// </summary>
     146    /// <param name="obj"></param>
     147    /// <returns></returns>
    112148    public static IEnumerable<DataMemberAccessor> GetStorableAccessors(object obj) {     
    113149      foreach (var memberInfo in GetStorableMembers(obj.GetType()))
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableConstructorAttribute.cs

    r2991 r2994  
    88namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable {
    99
     10
     11  /// <summary>
     12  /// Indicate that this constructor should be used instead of the default constructor
     13  /// when the <code>StorableSerializer</code> instantiates this class during
     14  /// deserialization.
     15  ///
     16  /// The constructor must take exactly one <code>bool</code> argument that will be
     17  /// set to <code>true</code> during deserialization.
     18  /// </summary>
    1019  [AttributeUsage(AttributeTargets.Constructor, Inherited = false, AllowMultiple = false)]
    1120  public sealed class StorableConstructorAttribute : Attribute {
     
    1726      new Dictionary<Type, ConstructorInfo>();
    1827
     28
     29    /// <summary>
     30    /// Get a designated storable constructor for a type or <code>null</code>.
     31    /// </summary>   
    1932    public static ConstructorInfo GetStorableConstructor(Type type) {
    2033      lock (constructorCache) {
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableHookAttribute.cs

    r2991 r2994  
    77namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable {
    88
     9
     10  /// <summary>
     11  /// Indicates the time at which the hook should be invoked.
     12  /// </summary>
    913  public enum HookType { BeforeSerialization, AfterDeserialization };
    1014
    11   [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
     15
     16  /// <summary>
     17  /// Mark methods that should be called at certain times during
     18  /// serialization/deserialization by the <code>StorableSerializer</code>.
     19  /// </summary>
     20  [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
    1221  public sealed class StorableHookAttribute : Attribute {
    1322
     
    2736    }
    2837
     38
     39    /// <summary>
     40    /// Mark method as <code>StorableSerializer</code> hook to be run
     41    /// at the <code>HookType</code> time.
     42    /// </summary>
     43    /// <param name="hookType"></param>
    2944    public StorableHookAttribute(HookType hookType) {
    3045      this.hookType = hookType;
     
    3954      new Dictionary<HookDesignator, List<MethodInfo>>();
    4055
     56
     57    /// <summary>
     58    /// Invoke <code>hookType</code> hook on <code>obj</code>.
     59    /// </summary>   
    4160    public static void InvokeHook(HookType hookType, object obj) {
    4261      if (obj == null)
     
    4564        mi.Invoke(obj, emptyArgs);
    4665      }
    47     }
     66    }   
    4867
    4968    private static IEnumerable<MethodInfo> GetHooks(HookType hookType, Type type) {
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs

    r2993 r2994  
    99namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable {
    1010
    11   [EmptyStorableClass]
     11  /// <summary>
     12  /// Intended for serialization of all custom classes. Classes should have the
     13  /// <code>[StorableClass(StorableClassType.Empty)]</code> attribute set and a serialization mode set.
     14  /// Optionally selected fields and properties can be marked with the
     15  /// <code>[Storable]</code> attribute.
     16  /// </summary>
     17  [StorableClass(StorableClassType.Empty)]   
    1218  public class StorableSerializer : ICompositeSerializer {
    1319
     
    2026        StorableConstructorAttribute.GetStorableConstructor(type) == null)
    2127        return false;
    22       while (type != null) {
    23         if (StorableAttribute.GetStorableMembers(type, false).Count() == 0 &&
    24             !EmptyStorableClassAttribute.IsEmptyStorable(type))
    25           return false;
    26         type = type.BaseType;
    27       }
    28       return true;
     28      return StorableClassAttribute.IsStorableType(type, true);
    2929    }
    3030
     
    3333        StorableConstructorAttribute.GetStorableConstructor(type) == null)
    3434        return "no default constructor and no storable constructor";
    35       while (type != null) {
    36         if (StorableAttribute.GetStorableMembers(type, false).Count() == 0 &&
    37             !EmptyStorableClassAttribute.IsEmptyStorable(type))
    38           return string.Format("{0} has no storable members and is not marked [EmtpyStorableClass]",
    39             type);
    40         type = type.BaseType;
    41       }
    42       return "no reason";
     35      return "class or one of its base classes is not empty and has no [StorableClass(StorableClassType.Empty)] attribute";
    4336    }
    4437
     
    5952      try {
    6053        ConstructorInfo constructor = StorableConstructorAttribute.GetStorableConstructor(type);
    61         return constructor != null ? constructor.Invoke(defaultArgs) :  Activator.CreateInstance(type, true);       
     54        return constructor != null ? constructor.Invoke(defaultArgs) : Activator.CreateInstance(type, true);
    6255      } catch (TargetInvocationException x) {
    6356        throw new PersistenceException(
     
    7265      while (iter.MoveNext()) {
    7366        memberDict.Add(iter.Current.Name, iter.Current);
    74       }
     67      }     
    7568      foreach (var accessor in StorableAttribute.GetStorableAccessors(instance)) {
    7669        if (memberDict.ContainsKey(accessor.Name)) {
Note: See TracChangeset for help on using the changeset viewer.