Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/20/17 15:56:55 (7 years ago)
Author:
gkronber
Message:

#2520 added versions to storable types and implemented conversion unit test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/StorableClassTransformer.cs

    r14713 r14771  
    7070      var type = obj.GetType();
    7171      var typeInfo = Mapper.StaticCache.GetTypeInfo(type);
     72      var typeBox = mapper.GetBox(box.TypeId).GetExtension(TypeBox.Type);
     73      var version = typeBox.HasVersion ? typeBox.Version : 1;
    7274
    7375      var components = new Dictionary<uint, uint>();
     
    7678      }
    7779
     80      var conversionMethods =
     81        type.GetMethods(BindingFlags.NonPublic | BindingFlags.Static)
     82          .Where(StorableConversionAttribute.IsStorableConversionMethod)
     83          .Where(mi => StorableConversionAttribute.GetVersion(mi) >= version)
     84          .OrderBy(StorableConversionAttribute.GetVersion);
     85
     86      // put all objects into dictionary for optional conversion
     87      var dict = new Dictionary<string, object>();
     88      foreach (var component in components) {
     89        dict.Add(mapper.GetString(component.Key), mapper.GetObject(component.Value));
     90      }
     91     
     92      // TODO: check that all entries in the dictionary can be mapped to a field or property
     93      foreach (var convMeth in conversionMethods) {
     94        dict = (Dictionary<string, object>)convMeth.Invoke(null, new object[] { dict });
     95      }
     96
    7897      foreach (var componentInfo in typeInfo.Fields) {
    7998        var field = (FieldInfo)componentInfo.MemberInfo;
    80         uint componentId;
    81         bool found = components.TryGetValue(mapper.GetStringId(componentInfo.Name), out componentId);
     99        object val = null;
     100        bool found = dict.TryGetValue(componentInfo.Name, out val);
    82101        if (found)
    83           field.SetValue(obj, mapper.GetObject(componentId));
     102          field.SetValue(obj, val);
    84103        else if (componentInfo.StorableAttribute.DefaultValue != null)
    85104          field.SetValue(obj, componentInfo.StorableAttribute.DefaultValue);
     
    88107      foreach (var componentInfo in typeInfo.Properties.Where(x => x.Writeable)) {
    89108        var property = (PropertyInfo)componentInfo.MemberInfo;
    90         uint componentId;
    91         bool found = components.TryGetValue(mapper.GetStringId(componentInfo.Name), out componentId);
     109        object val = null;
     110        bool found = dict.TryGetValue(componentInfo.Name, out val);
    92111        if (found)
    93           property.SetValue(obj, mapper.GetObject(componentId), null);
     112          property.SetValue(obj, val, null);
    94113        else if (componentInfo.StorableAttribute.DefaultValue != null)
    95114          property.SetValue(obj, componentInfo.StorableAttribute.DefaultValue, null);
Note: See TracChangeset for help on using the changeset viewer.