- Timestamp:
- 05/19/09 17:07:55 (16 years ago)
- 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 6 6 7 7 public class DataMemberAccessor { 8 9 public readonly Func<object> Get; 8 9 public readonly Func<object> Get; 10 10 public readonly Action<object> Set; 11 11 public readonly string Name; -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/EmptyStorableClassAttribute.cs
r1823 r1852 4 4 5 5 namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable { 6 7 6 8 7 [AttributeUsage( … … 14 13 private static readonly Dictionary<Type, bool> emptyTypeInfo = new Dictionary<Type, bool>(); 15 14 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> 16 22 public static bool IsEmptyStorable(Type type) { 17 23 if (emptyTypeInfo.ContainsKey(type)) … … 20 26 return true; 21 27 } 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) { 25 30 emptyTypeInfo.Add(type, true); 26 31 return true; 27 32 } 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; 41 51 } 42 52 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableAttribute.cs
r1823 r1852 26 26 public static IEnumerable<KeyValuePair<StorableAttribute, MemberInfo>> 27 27 GetStorableMembers(Type type, bool inherited) { 28 if ( type.BaseType != null)28 if (inherited && type.BaseType != null) 29 29 foreach (var pair in GetStorableMembers(type.BaseType)) 30 30 yield return pair; -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs
r1823 r1852 17 17 18 18 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; 23 28 } 24 29
Note: See TracChangeset
for help on using the changeset viewer.