Line | |
---|
1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Reflection;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Persistence.Core {
|
---|
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 | public static bool IsEmptyStorable(Type type) {
|
---|
16 | if (emptyTypeInfo.ContainsKey(type))
|
---|
17 | return emptyTypeInfo[type];
|
---|
18 | foreach (var attribute in type.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 | }
|
---|
39 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.