Changeset 3025 for trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableClassAttribute.cs
- Timestamp:
- 03/15/10 00:00:12 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableClassAttribute.cs
r3017 r3025 69 69 } 70 70 71 /// <summary>72 /// Check that the type is either empty i.e. has no fields or properties73 /// 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 = type84 .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 else91 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 except100 /// 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 else118 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 }128 71 } 129 72 } 130 73 131 132
Note: See TracChangeset
for help on using the changeset viewer.