Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/15/10 00:00:12 (15 years ago)
Author:
epitzer
Message:

Move attribute discovery from attribute classes to StorableSerializer (in preparation for unified discovery and handling of AllFields, AllProperties, ...) (#548)

File:
1 edited

Legend:

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

    r3017 r3025  
    6969    }
    7070
    71     /// <summary>
    72     /// Check that the type is either empty i.e. has no fields or properties
    73     /// or conatins proper parameterization through the storable attribute.
    74     /// </summary>
    75     /// <param name="type">The type.</param>
    76     /// <param name="recusrive">if set to <c>true</c> recusrively checks class hierarchy.</param>
    77     /// <returns>
    78     /// <c>true</c> if the specified type is a storable type; otherwise, <c>false</c>.
    79     /// </returns>
    80     public static bool IsStorableType(Type type, bool recusrive) {
    81       if (IsEmptyType(type, recusrive))
    82         return true;
    83       StorableClassAttribute attribute = type
    84         .GetCustomAttributes(typeof(StorableClassAttribute), false)
    85         .Cast<StorableClassAttribute>().SingleOrDefault();
    86       if (attribute == null)
    87         return false;
    88       if (!recusrive || type.BaseType == null)
    89         return true;
    90       else
    91         return IsStorableType(type.BaseType, true);
    92     }
    93 
    94     private const BindingFlags allDeclaredMembers =
    95       BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;
    96 
    97 
    98     /// <summary>
    99     /// Determines whether the specified type has no fields or properties except
    100     /// readonly properties or constant fields.
    101     /// </summary>
    102     /// <param name="type">The type.</param>
    103     /// <param name="recursive">if set to <c>true</c> recursively check class hierarchy.</param>
    104     /// <returns>
    105     /// <c>true</c> if the specified type is empty; otherwise, <c>false</c>.
    106     /// </returns>
    107     public static bool IsEmptyType(Type type, bool recursive) {
    108       foreach (MemberInfo memberInfo in type.GetMembers(allDeclaredMembers)) {
    109         if (
    110           memberInfo.MemberType == MemberTypes.Field && IsModifiableField((FieldInfo)memberInfo) ||
    111           memberInfo.MemberType == MemberTypes.Property && IsModifiableProperty((PropertyInfo)memberInfo)) {
    112           return false;
    113         }
    114       }
    115       if (!recursive || type.BaseType == null)
    116         return true;
    117       else
    118         return IsEmptyType(type.BaseType, true);
    119     }
    120 
    121     private static bool IsModifiableField(FieldInfo fi) {
    122       return !fi.IsLiteral && !fi.IsInitOnly;
    123     }
    124 
    125     private static bool IsModifiableProperty(PropertyInfo pi) {
    126       return pi.CanWrite;
    127     }
    12871  }
    12972}
    13073
    131 
    132  
Note: See TracChangeset for help on using the changeset viewer.