Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2980 for trunk


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

add a new attribute for hooks to be called before serialization and after deserialization (#900)

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

Legend:

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

    r1960 r2980  
    2929
    3030    public IEnumerable<Tag> CreateMetaInfo(object o) {
     31      StorableHookAttribute.InvokeHook(HookType.BeforeSerialization, o);
    3132      return new Tag[] { };
    3233    }
     
    4849        memberDict.Add(iter.Current.Name, iter.Current);
    4950      }
    50       foreach (var accessor in StorableAttribute.GetStorableAccessors(instance)) {       
     51      foreach (var accessor in StorableAttribute.GetStorableAccessors(instance)) {
    5152        if (memberDict.ContainsKey(accessor.Name)) {
    52           accessor.Set(memberDict[accessor.Name].Value);         
     53          accessor.Set(memberDict[accessor.Name].Value);
    5354        } else if (accessor.DefaultValue != null) {
    54           accessor.Set(accessor.DefaultValue);         
     55          accessor.Set(accessor.DefaultValue);
    5556        }
    5657      }
     58      StorableHookAttribute.InvokeHook(HookType.AfterDeserialization, instance);
    5759    }
    5860  }
  • trunk/sources/HeuristicLab.Persistence/3.3/HeuristicLab.Persistence-3.3.csproj

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

    r2951 r2980  
    659659    }
    660660
     661    private class PersistenceHooks {
     662      [Storable]
     663      public int a;
     664      [Storable]
     665      public int b;
     666      public int sum;
     667      public bool WasSerialized { get; private set; }
     668      [StorableHook(HookType.BeforeSerialization)]
     669      void PreSerializationHook() {
     670        WasSerialized = true;
     671      }
     672      [StorableHook(HookType.AfterDeserialization)]
     673      void PostDeserializationHook() {
     674        sum = a + b;
     675      }
     676    }
     677
     678    [TestMethod]
     679    public void HookTest() {
     680      PersistenceHooks hookTest = new PersistenceHooks();
     681      hookTest.a = 2;
     682      hookTest.b = 5;
     683      Assert.IsFalse(hookTest.WasSerialized);
     684      Assert.AreEqual(hookTest.sum, 0);
     685      XmlGenerator.Serialize(hookTest, tempFile);
     686      Assert.IsTrue(hookTest.WasSerialized);
     687      Assert.AreEqual(hookTest.sum, 0);
     688      PersistenceHooks newHookTest = (PersistenceHooks)XmlParser.Deserialize(tempFile);
     689      Assert.AreEqual(newHookTest.a, hookTest.a);
     690      Assert.AreEqual(newHookTest.b, hookTest.b);
     691      Assert.AreEqual(newHookTest.sum, newHookTest.a + newHookTest.b);
     692      Assert.IsFalse(newHookTest.WasSerialized);
     693    }
     694
    661695    [ClassInitialize]
    662696    public static void Initialize(TestContext testContext) {
Note: See TracChangeset for help on using the changeset viewer.