Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/Storable/EmptyStorableClassAttribute.cs @ 1623

Last change on this file since 1623 was 1623, checked in by epitzer, 15 years ago

Namespace refactoring, visibility check (#548)

File size: 1.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4
5namespace HeuristicLab.Persistence.Default.Decomposers.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      foreach (var attribute in type.GetCustomAttributes(false)) {
20        EmptyStorableClassAttribute empty = attribute as EmptyStorableClassAttribute;
21        if (empty != null) {
22          emptyTypeInfo.Add(type, true);
23          return true;
24        }
25      }
26      int nFields = 0;
27      foreach (MemberInfo memberInfo in type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) {
28        if (memberInfo.MemberType == MemberTypes.Field ||
29          memberInfo.MemberType == MemberTypes.Property)
30          nFields += 1;
31      }
32      if (nFields == 0) {
33        emptyTypeInfo.Add(type, true);
34        return true;
35      }
36      emptyTypeInfo.Add(type, false);
37      return false;
38    }
39  }
40}
Note: See TracBrowser for help on using the repository browser.