Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10958


Ignore:
Timestamp:
06/06/14 14:12:39 (11 years ago)
Author:
epitzer
Message:

#2192 make StorableSerializer.HookDesignator derive from Tuple and inherit proper Equals() and GetHashCode()

File:
1 edited

Legend:

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

    r9456 r10958  
    4747      hookCache = new Dictionary<HookDesignator, List<StorableReflection.Hook>>();
    4848    }
     49
    4950    [StorableConstructor]
    5051    private StorableSerializer(bool deserializing) : this() { }
     
    6869    /// </returns>
    6970    public bool CanSerialize(Type type) {
    70       bool markedStorable = StorableReflection.HasStorableClassAttribute(type);
     71      var markedStorable = StorableReflection.HasStorableClassAttribute(type);
    7172      if (GetConstructor(type) == null)
    7273        if (markedStorable)
     
    9192    /// </returns>
    9293    public string JustifyRejection(Type type) {
    93       StringBuilder sb = new StringBuilder();
     94      var sb = new StringBuilder();
    9495      if (GetConstructor(type) == null)
    9596        sb.Append("class has no default constructor and no [StorableConstructor]");
     
    117118    /// <returns>An enumerable of <see cref="Tag"/>s.</returns>
    118119    public IEnumerable<Tag> Decompose(object obj) {
    119       foreach (var accessor in GetStorableAccessors(obj.GetType())) {
    120         if (accessor.Get != null)
    121           yield return new Tag(accessor.Name, accessor.Get(obj));
    122       }
     120      return from accessor in GetStorableAccessors(obj.GetType())
     121             where accessor.Get != null
     122             select new Tag(accessor.Name, accessor.Get(obj));
    123123    }
    124124
     
    147147    public void Populate(object instance, IEnumerable<Tag> objects, Type type) {
    148148      var memberDict = new Dictionary<string, Tag>();
    149       IEnumerator<Tag> iter = objects.GetEnumerator();
     149      var iter = objects.GetEnumerator();
    150150      while (iter.MoveNext()) {
    151151        memberDict.Add(iter.Current.Name, iter.Current);
     
    170170      BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
    171171
    172     private static readonly object[] emptyArgs = new object[] { };
    173     private static readonly object[] trueArgs = new object[] { true };
    174 
    175     private sealed class HookDesignator {
    176       public Type Type { get; private set; }
    177       public HookType HookType { get; private set; }
    178       public HookDesignator() { }
    179       public HookDesignator(Type type, HookType hookType) {
    180         Type = type;
    181         HookType = HookType;
    182       }
     172    private sealed class HookDesignator : Tuple<Type, HookType> {
     173      public HookDesignator(Type type, HookType hookType) : base(type, hookType) { }
    183174    }
    184175
     
    191182    #region caches
    192183
    193     private AccessorListCache accessorListCache;
    194     private AccessorCache accessorCache;
    195     private Dictionary<Type, Constructor> constructorCache;
    196     private Dictionary<HookDesignator, List<StorableReflection.Hook>> hookCache;
     184    private readonly AccessorListCache accessorListCache;
     185    private readonly AccessorCache accessorCache;
     186    private readonly Dictionary<Type, Constructor> constructorCache;
     187    private readonly Dictionary<HookDesignator, List<StorableReflection.Hook>> hookCache;
    197188
    198189    #endregion
     
    206197        var storableMembers = StorableReflection
    207198          .GenerateStorableMembers(type)
    208           .Select(mi => GetMemberAccessor(mi));
     199          .Select(GetMemberAccessor)
     200          .ToList();
    209201        accessorListCache[type] = storableMembers;
    210202        return storableMembers;
     
    216208        if (accessorCache.ContainsKey(mi.MemberInfo))
    217209          return new DataMemberAccessor(accessorCache[mi.MemberInfo], mi.DisentangledName, mi.DefaultValue);
    218         DataMemberAccessor dma = new DataMemberAccessor(mi.MemberInfo, mi.DisentangledName, mi.DefaultValue);
     210        var dma = new DataMemberAccessor(mi.MemberInfo, mi.DisentangledName, mi.DefaultValue);
    219211        accessorCache[mi.MemberInfo] = dma;
    220212        return dma;
     
    226218        if (constructorCache.ContainsKey(type))
    227219          return constructorCache[type];
    228         Constructor c = FindStorableConstructor(type) ?? GetDefaultConstructor(type);
     220        var c = FindStorableConstructor(type) ?? GetDefaultConstructor(type);
    229221        constructorCache.Add(type, c);
    230222        return c;
     
    233225
    234226    private Constructor GetDefaultConstructor(Type type) {
    235       ConstructorInfo ci = type.GetConstructor(ALL_CONSTRUCTORS, null, Type.EmptyTypes, null);
     227      var ci = type.GetConstructor(ALL_CONSTRUCTORS, null, Type.EmptyTypes, null);
    236228      if (ci == null)
    237229        return null;
    238       DynamicMethod dm = new DynamicMethod("", typeof(object), null, type, true);
    239       ILGenerator ilgen = dm.GetILGenerator();
     230      var dm = new DynamicMethod("", typeof(object), null, type, true);
     231      var ilgen = dm.GetILGenerator();
    240232      ilgen.Emit(OpCodes.Newobj, ci);
    241233      ilgen.Emit(OpCodes.Ret);
     
    244236
    245237    private Constructor FindStorableConstructor(Type type) {
    246       foreach (ConstructorInfo ci in type.GetConstructors(ALL_CONSTRUCTORS)) {
    247         if (ci.GetCustomAttributes(typeof(StorableConstructorAttribute), false).Length > 0) {
    248           if (ci.GetParameters().Length != 1 ||
    249               ci.GetParameters()[0].ParameterType != typeof(bool))
    250             throw new PersistenceException("StorableConstructor must have exactly one argument of type bool");
    251           DynamicMethod dm = new DynamicMethod("", typeof(object), null, type, true);
    252           ILGenerator ilgen = dm.GetILGenerator();
    253           ilgen.Emit(OpCodes.Ldc_I4_1); // load true
    254           ilgen.Emit(OpCodes.Newobj, ci);
    255           ilgen.Emit(OpCodes.Ret);
    256           return (Constructor)dm.CreateDelegate(typeof(Constructor));
    257         }
     238      foreach (var ci in type
     239        .GetConstructors(ALL_CONSTRUCTORS)
     240        .Where(ci => ci.GetCustomAttributes(typeof(StorableConstructorAttribute), false).Length > 0)) {
     241        if (ci.GetParameters().Length != 1 ||
     242            ci.GetParameters()[0].ParameterType != typeof(bool))
     243          throw new PersistenceException("StorableConstructor must have exactly one argument of type bool");
     244        var dm = new DynamicMethod("", typeof(object), null, type, true);
     245        var ilgen = dm.GetILGenerator();
     246        ilgen.Emit(OpCodes.Ldc_I4_1); // load true
     247        ilgen.Emit(OpCodes.Newobj, ci);
     248        ilgen.Emit(OpCodes.Ret);
     249        return (Constructor)dm.CreateDelegate(typeof(Constructor));
    258250      }
    259251      return null;
     
    262254    private void InvokeHook(HookType hookType, object obj) {
    263255      if (obj == null)
    264         throw new ArgumentNullException("Cannot invoke hooks on null");
    265       foreach (StorableReflection.Hook hook in GetHooks(hookType, obj.GetType())) {
     256        throw new ArgumentNullException("obj");
     257      foreach (var hook in GetHooks(hookType, obj.GetType())) {
    266258        hook(obj);
    267259      }
Note: See TracChangeset for help on using the changeset viewer.