Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/09/10 18:57:40 (15 years ago)
Author:
epitzer
Message:

add support for new StorableConstructor attribute (#900)

Location:
trunk/sources/HeuristicLab.Persistence/3.3
Files:
1 added
4 edited

Legend:

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

    r2980 r2983  
    3131      Type type = obj.GetType();
    3232      foreach (MemberInfo memberInfo in type.GetMembers(instanceMembers)) {
    33         foreach (object attribute in memberInfo.GetCustomAttributes(false)) {
    34           StorableHookAttribute hook = attribute as StorableHookAttribute;
     33        foreach (StorableHookAttribute hook in memberInfo.GetCustomAttributes(typeof(StorableHookAttribute), false)) {         
    3534          if (hook != null && hook.HookType == hookType) {
    3635            MethodInfo methodInfo = memberInfo as MethodInfo;
    3736            if (memberInfo.MemberType != MemberTypes.Method || memberInfo == null)
    38               throw new ArgumentException("Storable hooks must be methods");           
    39             methodInfo.Invoke(obj, emptyArgs);           
     37              throw new ArgumentException("Storable hooks must be methods");
     38            methodInfo.Invoke(obj, emptyArgs);
    4039          }
    4140        }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs

    r2980 r2983  
    4040
    4141    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
    42       return Activator.CreateInstance(type, true);
     42      object instance = StorableConstructorAttribute.CallStorableConstructor(type);
     43      if (instance == null)
     44        instance = Activator.CreateInstance(type, true);
     45      return instance;
    4346    }
    4447
  • trunk/sources/HeuristicLab.Persistence/3.3/HeuristicLab.Persistence-3.3.csproj

    r2980 r2983  
    121121    <Compile Include="Default\CompositeSerializers\Storable\EmptyStorableClassAttribute.cs" />
    122122    <Compile Include="Default\CompositeSerializers\Storable\StorableAttribute.cs" />
     123    <Compile Include="Default\CompositeSerializers\Storable\StorableConstructorAttribute.cs" />
    123124    <Compile Include="Default\CompositeSerializers\Storable\StorableHookAttribute.cs" />
    124125    <Compile Include="Default\CompositeSerializers\Storable\StorableSerializer.cs" />
  • trunk/sources/HeuristicLab.Persistence/3.3/Tests/UseCases.cs

    r2980 r2983  
    692692      Assert.IsFalse(newHookTest.WasSerialized);
    693693    }
     694   
     695    [EmptyStorableClass]
     696    private class CustomConstructor {
     697      public string Value = "none";
     698      public CustomConstructor() {
     699        Value = "default";
     700      }
     701      [StorableConstructor]
     702      private CustomConstructor(bool deserializing) {
     703        Assert.IsTrue(deserializing);
     704        Value = "persistence";
     705      }
     706    }
     707
     708    [TestMethod]
     709    public void TestCustomConstructor() {
     710      CustomConstructor cc = new CustomConstructor();
     711      Assert.AreEqual(cc.Value, "default");
     712      XmlGenerator.Serialize(cc, tempFile);
     713      CustomConstructor newCC = (CustomConstructor)XmlParser.Deserialize(tempFile);
     714      Assert.AreEqual(newCC.Value, "persistence");
     715    }
    694716
    695717    [ClassInitialize]
Note: See TracChangeset for help on using the changeset viewer.