Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/EmptyStorableClassAttribute.cs @ 1843

Last change on this file since 1843 was 1823, checked in by epitzer, 16 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

File size: 1.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4
5namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable {
6
7
8  [AttributeUsage(
9    AttributeTargets.Class,
10    AllowMultiple = false,
11    Inherited = false)]
12  public class EmptyStorableClassAttribute : Attribute {
13
14    private static readonly Dictionary<Type, bool> emptyTypeInfo = new Dictionary<Type, bool>();
15
16    public static bool IsEmptyStorable(Type type) {
17      if (emptyTypeInfo.ContainsKey(type))
18        return emptyTypeInfo[type];
19      if (type == typeof(object)) {
20        return true;
21      }
22      foreach (var attribute in type.GetCustomAttributes(false)) {
23        EmptyStorableClassAttribute empty = attribute as EmptyStorableClassAttribute;
24        if (empty != null) {
25          emptyTypeInfo.Add(type, true);
26          return true;
27        }
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;
41    }
42  }
43}
Note: See TracBrowser for help on using the repository browser.