Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/18/11 15:50:23 (13 years ago)
Author:
epitzer
Message:

Implement one-way serialization that allows either only loading or only saving of storable members by setting a new property called AllowOneWay on the [Storable] attribute (#1385)

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

Legend:

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

    r4068 r5324  
    5656
    5757    /// <summary>
     58    /// Allow storable attribute on properties with only a getter or a setter. These
     59    /// properties will then by either only serialized but not deserialized or only
     60    /// deserialized (if stored) but not serialized again.
     61    /// </summary>
     62    public bool AllowOneWay { get; set; }
     63
     64    /// <summary>
    5865    /// Returns a <see cref="System.String"/> that represents this instance.
    5966    /// </summary>
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableMemberInfo.cs

    r4068 r5324  
    2323using System.Reflection;
    2424using System.Text;
     25using HeuristicLab.Persistence.Core;
    2526
    2627namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable {
     
    4344      DefaultValue = attribute.DefaultValue;
    4445      MemberInfo = memberInfo;
     46      if (!attribute.AllowOneWay)
     47        CheckPropertyAccess(memberInfo as PropertyInfo);
    4548    }
    46     public StorableMemberInfo(MemberInfo memberInfo) {
     49    public StorableMemberInfo(MemberInfo memberInfo, bool allowOneWay) {
    4750      MemberInfo = memberInfo;
     51      if (!allowOneWay)
     52        CheckPropertyAccess(memberInfo as PropertyInfo);
     53    }
     54    private static void CheckPropertyAccess(PropertyInfo propertyInfo) {
     55      if (propertyInfo == null)
     56        return;
     57      if (!propertyInfo.CanRead || !propertyInfo.CanWrite)
     58        throw new PersistenceException("Properties must be readable and writable or explicity enable one way serialization.");
    4859    }
    4960    public void SetDisentangledName(string name) {
     
    5162        DisentangledName = name;
    5263    }
     64    /// <summary>
     65    /// Gets the type who first defined this property in the class hierarchy when the
     66    /// property has subsequently been overridden but not shadowed with <code>new</code>.
     67    /// </summary>
     68    /// <returns>The properties base type.</returns>
    5369    public Type GetPropertyDeclaringBaseType() {
    54       return ((PropertyInfo)MemberInfo).GetGetMethod(true).GetBaseDefinition().DeclaringType;
     70      PropertyInfo pi = MemberInfo as PropertyInfo;
     71      if (pi == null)
     72        throw new PersistenceException("fields don't have a declaring base type, directly use FullyQualifiedMemberName instead");
     73      if (pi.CanRead)
     74        return pi.GetGetMethod(true).GetBaseDefinition().DeclaringType;
     75      if (pi.CanWrite)
     76        return pi.GetSetMethod(true).GetBaseDefinition().DeclaringType;
     77      throw new InvalidOperationException("property has neigher a getter nor a setter.");
    5578    }
    5679  }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableReflection.cs

    r4068 r5324  
    5858        }
    5959      }
    60 
    6160      return DisentangleNameMapping(storableMembers);
    6261    }
     
    108107            !memberInfo.Name.StartsWith("<") &&
    109108            !memberInfo.Name.EndsWith("k__BackingField"))
    110           storableMembers.Add(new StorableMemberInfo(memberInfo));
    111       }
    112     }
    113 
    114 
     109          storableMembers.Add(new StorableMemberInfo(memberInfo, false));
     110      }
     111    }
     112
     113    /// <summary>
     114    /// Ascertain distinct names for all fields and properties. This method takes care
     115    /// of disentangling equal names from different class hiarachy levels.
     116    ///
     117    /// Field names are replaced with their fully qualified name which includes
     118    /// the class names where they were declared.
     119    ///
     120    /// Property names are first reduced to unqiue accessors that are not overrides of
     121    /// each other and the replaced with their fully qualified name if more than one
     122    /// accessor remains.
     123    /// </summary>
     124    /// <param name="storableMemberInfos"></param>
     125    /// <returns></returns>
    115126    private static IEnumerable<StorableMemberInfo> DisentangleNameMapping(
    116127        IEnumerable<StorableMemberInfo> storableMemberInfos) {
     
    138149    }
    139150
     151    /// <summary>
     152    /// Merges property accessors that are overrides of each other but differentiates if a new
     153    /// property that shadows older implementations has been introduced with <code>new</code>.
     154    /// </summary>
     155    /// <param name="members">A list of <code>StorableMemberInfo</code>s for properties of the same type.</param>
     156    /// <returns>A fieltered <code>IEnumerable</code> of propery infos.</returns>
    140157    private static IEnumerable<StorableMemberInfo> MergePropertyAccessors(List<StorableMemberInfo> members) {
    141158      var uniqueAccessors = new Dictionary<Type, StorableMemberInfo>();
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs

    r5292 r5324  
    118118    public IEnumerable<Tag> Decompose(object obj) {
    119119      foreach (var accessor in GetStorableAccessors(obj.GetType())) {
    120         yield return new Tag(accessor.Name, accessor.Get(obj));
     120        if (accessor.Get != null)
     121          yield return new Tag(accessor.Name, accessor.Get(obj));
    121122      }
    122123    }
     
    151152      }
    152153      foreach (var accessor in GetStorableAccessors(instance.GetType())) {
    153         if (memberDict.ContainsKey(accessor.Name)) {
    154           accessor.Set(instance, memberDict[accessor.Name].Value);
    155         } else if (accessor.DefaultValue != null) {
    156           accessor.Set(instance, accessor.DefaultValue);
     154        if (accessor.Set != null) {
     155          if (memberDict.ContainsKey(accessor.Name)) {
     156            accessor.Set(instance, memberDict[accessor.Name].Value);
     157          } else if (accessor.DefaultValue != null) {
     158            accessor.Set(instance, accessor.DefaultValue);
     159          }
    157160        }
    158161      }
Note: See TracChangeset for help on using the changeset viewer.