1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 |
|
---|
25 | namespace HeuristicLab.Persistence {
|
---|
26 | /// <summary>
|
---|
27 | /// Mark a class to be considered by the <c>StorableSerializer</c>.
|
---|
28 | /// </summary>
|
---|
29 | [AttributeUsage(
|
---|
30 | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Delegate,
|
---|
31 | Inherited = false,
|
---|
32 | AllowMultiple = false
|
---|
33 | )]
|
---|
34 | public sealed class StorableTypeAttribute : Attribute {
|
---|
35 | private static IDictionary<Type, StorableTypeAttribute> attributeCache = new Dictionary<Type, StorableTypeAttribute>();
|
---|
36 |
|
---|
37 | #region Properties
|
---|
38 | /// <summary>
|
---|
39 | /// Specify how members are selected for serialization.
|
---|
40 | /// </summary>
|
---|
41 | public StorableMemberSelection MemberSelection { get; private set; }
|
---|
42 |
|
---|
43 | /// <summary>
|
---|
44 | /// The GUID that identifies the type.
|
---|
45 | /// </summary>
|
---|
46 | /// <value>The GUID.</value>
|
---|
47 | public Guid Guid { get; private set; }
|
---|
48 | #endregion
|
---|
49 |
|
---|
50 | /// <summary>
|
---|
51 | /// Mark a class to be serialize by the <c>StorableSerizlier</c>
|
---|
52 | /// </summary>
|
---|
53 | /// <param name="memberSelection">The storable class memberSelection.</param>
|
---|
54 | public StorableTypeAttribute(StorableMemberSelection memberSelection, string guid) {
|
---|
55 | MemberSelection = memberSelection;
|
---|
56 | Guid = new Guid(guid);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public StorableTypeAttribute(string guid) {
|
---|
60 | Guid = new Guid(guid);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /// <summary>
|
---|
64 | /// Initializes a new instance of the <see cref="StorableTypeAttribute"/> class.
|
---|
65 | /// The default value for <see cref="StorableMemberSelection"/> is
|
---|
66 | /// <see cref="StorableMemberSelection.MarkedOnly"/>.
|
---|
67 | /// </summary>
|
---|
68 | private StorableTypeAttribute() { }
|
---|
69 |
|
---|
70 | /// <summary>
|
---|
71 | /// Checks if the <see cref="StorableTypeAttribute"/> is present on a memberSelection.
|
---|
72 | /// </summary>
|
---|
73 | /// <param name="type">The memberSelection which should be checked for the <see cref="StorableTypeAttribute"/></param>
|
---|
74 | /// <returns></returns>
|
---|
75 | public static bool IsStorableType(Type type) {
|
---|
76 | return GetStorableTypeAttribute(type) != null;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public static StorableTypeAttribute GetStorableTypeAttribute(Type type) {
|
---|
80 | StorableTypeAttribute attrib;
|
---|
81 |
|
---|
82 | if (!attributeCache.TryGetValue(type, out attrib)) {
|
---|
83 | attrib = (StorableTypeAttribute)GetCustomAttribute(type, typeof(StorableTypeAttribute), false);
|
---|
84 | if (attrib != null) attributeCache[type] = attrib;
|
---|
85 | }
|
---|
86 |
|
---|
87 | return attrib;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|