Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/10/10 18:28:50 (14 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
Files:
1 added
1 deleted
27 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/Configuration.cs

    r1823 r2994  
    66namespace HeuristicLab.Persistence.Core {
    77
     8  [StorableClass(StorableClassType.MarkedOnly)]   
    89  public class Configuration {
    910
     
    1718    [Storable]
    1819    public IFormat Format { get; private set; }
    19 
     20   
    2021    private Configuration() {
    2122      compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>();
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/FormatBase.cs

    r1853 r2994  
    44namespace HeuristicLab.Persistence.Interfaces {
    55
    6   [EmptyStorableClass]
     6  [StorableClass(StorableClassType.Empty)]
    77  public abstract class FormatBase<SerialDataFormat> : IFormat<SerialDataFormat> where SerialDataFormat : ISerialData {
    88
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/PrimitiveSerializerBase.cs

    r1853 r2994  
    55namespace HeuristicLab.Persistence.Interfaces {
    66
    7   [EmptyStorableClass]
     7  [StorableClass(StorableClassType.Empty)]
    88  public abstract class PrimitiveSerializerBase<Source, SerialData> :
    99      IPrimitiveSerializer<Source, SerialData>
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/ArraySerializer.cs

    r2993 r2994  
    88namespace HeuristicLab.Persistence.Default.CompositeSerializers {
    99
    10   [EmptyStorableClass]
     10  [StorableClass(StorableClassType.Empty)]
    1111  public class ArraySerializer : ICompositeSerializer {
    1212
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/CompactNumberArray2StringSerializer.cs

    r2993 r2994  
    1010namespace HeuristicLab.Persistence.Default.CompositeSerializers {
    1111
    12   [EmptyStorableClass]
     12  [StorableClass(StorableClassType.Empty)]
    1313  public class CompactNumberArray2StringSerializer : ICompositeSerializer {
    1414
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/DictionarySerializer.cs

    r2993 r2994  
    99namespace HeuristicLab.Persistence.Default.CompositeSerializers {
    1010
    11   [EmptyStorableClass]
     11  [StorableClass(StorableClassType.Empty)]
    1212  public class DictionarySerializer : ICompositeSerializer {
    1313
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/EnumSerializer.cs

    r2993 r2994  
    77namespace HeuristicLab.Persistence.Default.CompositeSerializers {
    88
    9   [EmptyStorableClass]
     9  [StorableClass(StorableClassType.Empty)]
    1010  public class EnumSerializer : ICompositeSerializer {
    1111
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/EnumerableSerializer.cs

    r2993 r2994  
    1010namespace HeuristicLab.Persistence.Default.CompositeSerializers {
    1111
    12   [EmptyStorableClass]
     12  [StorableClass(StorableClassType.Empty)]
    1313  public class EnumerableSerializer : ICompositeSerializer {
    1414
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/KeyValuePairSerializer.cs

    r2993 r2994  
    99namespace HeuristicLab.Persistence.Default.CompositeSerializers {
    1010
    11   [EmptyStorableClass]
     11  [StorableClass(StorableClassType.Empty)]
    1212  public class KeyValuePairSerializer : ICompositeSerializer {
    1313
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Number2StringSerializer.cs

    r2993 r2994  
    1212namespace HeuristicLab.Persistence.Default.CompositeSerializers {
    1313
    14   [EmptyStorableClass]
     14  [StorableClass(StorableClassType.Empty)]
    1515  public class Number2StringSerializer : ICompositeSerializer {
    1616
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/NumberEnumerable2StringSerializer.cs

    r2993 r2994  
    1111namespace HeuristicLab.Persistence.Default.CompositeSerializers {
    1212
    13   [EmptyStorableClass]
     13  [StorableClass(StorableClassType.Empty)]
    1414  public class NumberEnumerable2StringSerializer : ICompositeSerializer {
    1515
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/StackSerializer.cs

    r2993 r2994  
    1010namespace HeuristicLab.Persistence.Default.CompositeSerializers {
    1111
    12   [EmptyStorableClass]
     12  [StorableClass(StorableClassType.Empty)]
    1313  public class StackSerializer : ICompositeSerializer {
    1414
  • 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)) {
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/StructSerializer.cs

    r2993 r2994  
    1010namespace HeuristicLab.Persistence.Default.CompositeSerializers {
    1111
    12   [EmptyStorableClass]
     12  [StorableClass(StorableClassType.Empty)]
    1313  public class StructSerializer : ICompositeSerializer {   
    1414
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/TypeSerializer.cs

    r2993 r2994  
    88namespace HeuristicLab.Persistence.Default.CompositeSerializers {
    99
    10   [EmptyStorableClass]
     10  [StorableClass(StorableClassType.Empty)]
    1111  public class TypeSerializer : ICompositeSerializer {
    1212
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/DebugString/DebugString.cs

    r1823 r2994  
    1010namespace HeuristicLab.Persistence.Default.DebugString {
    1111 
     12  [StorableClass(StorableClassType.MarkedOnly)] 
    1213  public class DebugString : ISerialData {
    1314
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/DebugString/DebugStringFormat.cs

    r1823 r2994  
    1010namespace HeuristicLab.Persistence.Default.DebugString {
    1111
    12   [EmptyStorableClass]
     12  [StorableClass(StorableClassType.Empty)]
    1313  public class DebugStringFormat : FormatBase<DebugString> {
    1414    public override string Name { get { return "DebugString"; } }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberArray2XmlSerializerBase.cs

    r1853 r2994  
    88namespace HeuristicLab.Persistence.Default.Xml.Compact {
    99
    10   [EmptyStorableClass]
     10  [StorableClass(StorableClassType.Empty)]
    1111  public abstract class NumberArray2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : class {
    1212
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberEnumeration2XmlSerializerBase.cs

    r1853 r2994  
    88namespace HeuristicLab.Persistence.Default.Xml.Compact {
    99
    10   [EmptyStorableClass]
     10  [StorableClass(StorableClassType.Empty)]
    1111  public abstract class NumberEnumeration2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : IEnumerable {
    1212
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlFormat.cs

    r1823 r2994  
    55namespace HeuristicLab.Persistence.Default.Xml {
    66
    7   [EmptyStorableClass]
     7  [StorableClass(StorableClassType.Empty)]
    88  public class XmlFormat : FormatBase<XmlString> {
    99    public override string Name { get { return "XML"; } }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlString.cs

    r1823 r2994  
    66namespace HeuristicLab.Persistence.Default.Xml {
    77
     8  [StorableClass(StorableClassType.MarkedOnly)] 
    89  public class XmlString : ISerialData {
    910
  • trunk/sources/HeuristicLab.Persistence/3.3/HeuristicLab.Persistence-3.3.csproj

    r2983 r2994  
    119119    <Compile Include="Default\CompositeSerializers\StackSerializer.cs" />
    120120    <Compile Include="Default\CompositeSerializers\Storable\DataMemberAccessor.cs" />
    121     <Compile Include="Default\CompositeSerializers\Storable\EmptyStorableClassAttribute.cs" />
     121    <Compile Include="Default\CompositeSerializers\Storable\StorableClassAttribute.cs" />
    122122    <Compile Include="Default\CompositeSerializers\Storable\StorableAttribute.cs" />
    123123    <Compile Include="Default\CompositeSerializers\Storable\StorableConstructorAttribute.cs" />
  • trunk/sources/HeuristicLab.Persistence/3.3/Tests/StorableAttributeTests.cs

    r2951 r2994  
    88namespace HeuristicLab.Persistence_33.Tests {
    99
     10  [StorableClass(StorableClassType.MarkedOnly)]
    1011  class DemoClass {
    1112
     
    2021  }
    2122
     23  [StorableClass(StorableClassType.MarkedOnly)]
    2224  class Base {
    2325    public string baseName;
     
    2931  }
    3032
     33  [StorableClass(StorableClassType.MarkedOnly)]
    3134  class Override : Base {
    3235    [Storable]
     
    3740  }
    3841
    39   [EmptyStorableClass]
     42  [StorableClass(StorableClassType.Empty)]
    4043  class Intermediate : Override {
    4144  }
    4245
     46  [StorableClass(StorableClassType.MarkedOnly)]
    4347  class New : Intermediate {
    4448    public string newName;
  • trunk/sources/HeuristicLab.Persistence/3.3/Tests/UseCases.cs

    r2993 r2994  
    2121namespace HeuristicLab.Persistence_33.Tests {
    2222
     23  [StorableClass(StorableClassType.MarkedOnly)] 
    2324  public class NumberTest {
    2425    [Storable]
     
    4243  }
    4344
     45  [StorableClass(StorableClassType.MarkedOnly)]
    4446  public class NonDefaultConstructorClass {
    4547    [Storable]
     
    5052  }
    5153
     54  [StorableClass(StorableClassType.MarkedOnly)]
    5255  public class IntWrapper {
    5356
     
    7275  }
    7376
     77  [StorableClass(StorableClassType.MarkedOnly)]
    7478  public class PrimitivesTest : NumberTest {
    7579    [Storable]
     
    8690  public enum TestEnum { va1, va2, va3, va8 } ;
    8791
     92  [StorableClass(StorableClassType.MarkedOnly)]
    8893  public class RootBase {
    8994    [Storable]
     
    9398  }
    9499
     100  [StorableClass(StorableClassType.MarkedOnly)]
    95101  public class Root : RootBase {
    96102    [Storable]
     
    127133  public enum TrickyEnum { zero = 0, one = 1, two = 2 }
    128134
     135  [StorableClass(StorableClassType.MarkedOnly)]
    129136  public class EnumTest {
    130137    [Storable]
     
    136143  }
    137144
     145  [StorableClass(StorableClassType.MarkedOnly)]
    138146  public class Custom {
    139147    [Storable]
     
    145153  }
    146154
     155  [StorableClass(StorableClassType.MarkedOnly)]
    147156  public class Manager {
    148157
     
    157166  }
    158167
     168  [StorableClass(StorableClassType.MarkedOnly)]
    159169  public class C {
    160170    [Storable]
     
    353363    }
    354364
     365    [StorableClass(StorableClassType.MarkedOnly)]
    355366    public class NestedType {
    356367      [Storable]
     
    582593    }
    583594
     595    [StorableClass(StorableClassType.MarkedOnly)]
    584596    class Child {
    585597      [Storable]
     
    587599    }
    588600
     601    [StorableClass(StorableClassType.MarkedOnly)]
    589602    class Parent {
    590603      [Storable]
     
    592605    }
    593606
     607    [StorableClass(StorableClassType.MarkedOnly)]
    594608    class GrandParent {
    595609      [Storable]
     
    659673    }
    660674
     675    [StorableClass(StorableClassType.MarkedOnly)]
    661676    private class PersistenceHooks {
    662677      [Storable]
     
    693708    }
    694709   
    695     [EmptyStorableClass]
     710    [StorableClass(StorableClassType.Empty)]
    696711    private class CustomConstructor {
    697712      public string Value = "none";
     
    715730    }
    716731
    717     [EmptyStorableClass]
     732    [StorableClass(StorableClassType.Empty)]
    718733    public class ExplodingDefaultConstructor {
    719734      public ExplodingDefaultConstructor() {
Note: See TracChangeset for help on using the changeset viewer.