Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1404


Ignore:
Timestamp:
03/24/09 16:57:25 (15 years ago)
Author:
epitzer
Message:

New attribute for legitimately empty but storable classes. (#506)

Location:
branches/New Persistence Exploration/Persistence/Persistence/Core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/Core/Serializer.cs

    r1362 r1404  
    132132        }
    133133      }
    134       if (nSubComponents == 0) {
     134      if (nSubComponents == 0 && ! EmptyStorableClassAttribute.IsEmpyStorable(value)) {
    135135        throw new ApplicationException(
    136136          String.Format(
    137             "CustomComposite value of type \"{0}\" contains no subcomponents",
     137            "Value of type \"{0}\" has no formatter or decomposer, " +
     138            "contains no storable subcomponents, " +
     139            "and is not marked as empty storable class.",
    138140            value.GetType().FullName));
    139141      }
  • branches/New Persistence Exploration/Persistence/Persistence/Core/StorableAttribute.cs

    r1360 r1404  
    44
    55namespace HeuristicLab.Persistence.Core {
     6
     7 
     8  [AttributeUsage(
     9    AttributeTargets.Class,
     10    AllowMultiple=false,
     11    Inherited=false)]
     12  public class EmptyStorableClassAttribute : Attribute {
     13    private static readonly Dictionary<Type, bool> emptyTypeInfo = new Dictionary<Type, bool>();
     14    public static bool IsEmpyStorable(object o) {
     15      Type type = o.GetType();
     16      if (emptyTypeInfo.ContainsKey(type))
     17        return emptyTypeInfo[type];
     18      foreach (var attribute in o.GetType().GetCustomAttributes(false)) {
     19        EmptyStorableClassAttribute empty = attribute as EmptyStorableClassAttribute;
     20        if (empty != null) {
     21          emptyTypeInfo.Add(type, true);
     22          return true;
     23        }
     24      }
     25      int nFields = 0;
     26      foreach ( MemberInfo memberInfo in type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ) {
     27        if (memberInfo.MemberType == MemberTypes.Field ||
     28          memberInfo.MemberType == MemberTypes.Property)
     29          nFields += 1;
     30      }
     31      if (nFields == 0) {
     32        emptyTypeInfo.Add(type, true);
     33        return true;
     34      }
     35      emptyTypeInfo.Add(type, false);
     36      return false;
     37    }
     38  }
    639
    740  [AttributeUsage(
Note: See TracChangeset for help on using the changeset viewer.