Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/19/09 17:07:55 (16 years ago)
Author:
epitzer
Message:

Correct handling of empty storable classes. (#603)

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/DataMemberAccessor.cs

    r1823 r1852  
    66
    77  public class DataMemberAccessor {
    8    
    9     public readonly Func<object> Get;   
     8
     9    public readonly Func<object> Get;
    1010    public readonly Action<object> Set;
    1111    public readonly string Name;
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/EmptyStorableClassAttribute.cs

    r1823 r1852  
    44
    55namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable {
    6 
    76
    87  [AttributeUsage(
     
    1413    private static readonly Dictionary<Type, bool> emptyTypeInfo = new Dictionary<Type, bool>();
    1514
     15    private const BindingFlags BINDING_FLAGS =
     16      BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;
     17
     18    /// <summary>
     19    /// Check if supplied type is empty (i.e. no properties or fields) or is marked as empty storable.
     20    /// This method is not recusrice and does not check if base types are also empty.
     21    /// </summary>   
    1622    public static bool IsEmptyStorable(Type type) {
    1723      if (emptyTypeInfo.ContainsKey(type))
     
    2026        return true;
    2127      }
    22       foreach (var attribute in type.GetCustomAttributes(false)) {
    23         EmptyStorableClassAttribute empty = attribute as EmptyStorableClassAttribute;
    24         if (empty != null) {
     28      foreach (var attribute in type.GetCustomAttributes(false)) {       
     29        if (attribute as EmptyStorableClassAttribute != null) {
    2530          emptyTypeInfo.Add(type, true);
    2631          return true;
    2732        }
    28       }
    29       int nFields = 0;
    30       foreach (MemberInfo memberInfo in type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) {
    31         if (memberInfo.MemberType == MemberTypes.Field ||
    32           memberInfo.MemberType == MemberTypes.Property)
    33           nFields += 1;
    34       }
    35       if (nFields == 0) {
    36         emptyTypeInfo.Add(type, true);
    37         return true;
    38       }
    39       emptyTypeInfo.Add(type, false);
    40       return false;
     33      }     
     34      foreach (MemberInfo memberInfo in type.GetMembers(BINDING_FLAGS)) {
     35        if (
     36          memberInfo.MemberType == MemberTypes.Field && IsModifiableField((FieldInfo)memberInfo) ||         
     37          memberInfo.MemberType == MemberTypes.Property && IsModifiableProperty((PropertyInfo)memberInfo) ) {
     38          emptyTypeInfo.Add(type, false);
     39          return false;
     40        }         
     41      }     
     42      emptyTypeInfo.Add(type, true);
     43      return true;           
     44    }
     45
     46    public static bool IsModifiableField(FieldInfo fi) {     
     47      return !fi.IsLiteral && !fi.IsInitOnly;
     48    }
     49    public static bool IsModifiableProperty(PropertyInfo pi) {
     50      return pi.CanWrite;
    4151    }
    4252  }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableAttribute.cs

    r1823 r1852  
    2626    public static IEnumerable<KeyValuePair<StorableAttribute, MemberInfo>>
    2727        GetStorableMembers(Type type, bool inherited) {
    28       if (type.BaseType != null)
     28      if (inherited && type.BaseType != null)
    2929        foreach (var pair in GetStorableMembers(type.BaseType))
    3030          yield return pair;
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs

    r1823 r1852  
    1717
    1818    public bool CanSerialize(Type type) {
    19       return ReflectionTools.HasDefaultConstructor(type) &&
    20         (StorableAttribute.GetStorableMembers(type, false).Count() > 0 ||
    21           EmptyStorableClassAttribute.IsEmptyStorable(type));
    22 
     19      if (!ReflectionTools.HasDefaultConstructor(type))
     20        return false;
     21      while (type != null) {       
     22        if (StorableAttribute.GetStorableMembers(type, false).Count() == 0 &&
     23            !EmptyStorableClassAttribute.IsEmptyStorable(type))
     24          return false;
     25        type = type.BaseType;
     26      }
     27      return true;
    2328    }
    2429
Note: See TracChangeset for help on using the changeset viewer.