Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/03/18 17:48:20 (6 years ago)
Author:
jkarder
Message:

#2520: worked on new persistence

  • added cache for different attributes
  • refactored StorableTypeBoxTransformer
  • implemented paths for Storable members
  • implemented more unit tests
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StorableAttribute.cs

    r15035 r16210  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Reflection;
    24 using System.Text;
    2525
    2626namespace HeuristicLab.Persistence {
    27 
    28 
    2927  /// <summary>
    3028  /// Mark the member of a class to be considered by the <c>StorableSerializer</c>.
     
    3634    AttributeTargets.Field | AttributeTargets.Property,
    3735    AllowMultiple = false,
    38     Inherited = false)]
    39   public class StorableAttribute : Attribute {
    40     public static bool IsStorable(MemberInfo memberInfo) {
    41       return Attribute.IsDefined(memberInfo, typeof(StorableAttribute), false);
    42     }
    43     public static StorableAttribute GetStorableAttribute(MemberInfo memberInfo) {
    44       return (StorableAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(StorableAttribute), false);
    45     }
     36    Inherited = false
     37  )]
     38  public sealed class StorableAttribute : Attribute {
     39    private static IDictionary<MemberInfo, StorableAttribute> attributeCache = new Dictionary<MemberInfo, StorableAttribute>();
    4640
     41    #region Properties
    4742    /// <summary>
    4843    /// An optional name for this member that will be used during serialization.
     
    6762    /// deserialized (setter only) but not serialized again.
    6863    /// </summary>
     64    [Obsolete("Use OldName instead.")]
    6965    public bool AllowOneWay { get; set; }
    7066
    71     /// <summary>
    72     /// Returns a <see cref="System.String"/> that represents this instance.
    73     /// </summary>
    74     /// <returns>
    75     /// A <see cref="System.String"/> that represents this instance.
    76     /// </returns>
    77     public override string ToString() {
    78       StringBuilder sb = new StringBuilder();
    79       sb.Append("[Storable");
    80       if (Name != null || DefaultValue != null)
    81         sb.Append('(');
    82       if (Name != null) {
    83         sb.Append("Name = \"").Append(Name).Append("\"");
    84         if (DefaultValue != null)
    85           sb.Append(", ");
     67    public string OldName { get; set; }
     68    #endregion
     69
     70    public static bool IsStorable(MemberInfo memberInfo) {
     71      return GetStorableAttribute(memberInfo) != null;
     72    }
     73    public static StorableAttribute GetStorableAttribute(MemberInfo memberInfo) {
     74      StorableAttribute attrib;
     75
     76      if (!attributeCache.TryGetValue(memberInfo, out attrib)) {
     77        attrib = (StorableAttribute)GetCustomAttribute(memberInfo, typeof(StorableAttribute), false);
     78        if (attrib != null) attributeCache[memberInfo] = attrib;
    8679      }
    87       if (DefaultValue != null)
    88         sb.Append("DefaultValue = \"").Append(DefaultValue).Append("\"");
    89       if (Name != null || DefaultValue != null)
    90         sb.Append(')');
    91       sb.Append(']');
    92       return sb.ToString();
     80
     81      return attrib;
    9382    }
    9483  }
Note: See TracChangeset for help on using the changeset viewer.