Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StorableAttribute.cs @ 15035

Last change on this file since 15035 was 15035, checked in by gkronber, 7 years ago

#2520: made some changes related to renaming of storable members

File size: 3.5 KB
Line 
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
22using System;
23using System.Reflection;
24using System.Text;
25
26namespace HeuristicLab.Persistence {
27
28
29  /// <summary>
30  /// Mark the member of a class to be considered by the <c>StorableSerializer</c>.
31  /// The class must be marked as <c>[StorableClass("05FE6F11-87C6-435E-800A-166AFACCF5AC")]</c> and the
32  /// <c>StorableMemberSelection</c> should be set to <c>MarkedOnly</c> for
33  /// this attribute to kick in.
34  /// </summary>
35  [AttributeUsage(
36    AttributeTargets.Field | AttributeTargets.Property,
37    AllowMultiple = false,
38    Inherited = false)]
39  public class StorableAttribute : Attribute {
40    public static bool IsStorable(MemberInfo memberInfo) {
41      return Attribute.IsDefined(memberInfo, typeof(StorableAttribute), false);
42    }
43    public static StorableAttribute GetStorableAttribute(MemberInfo memberInfo) {
44      return (StorableAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(StorableAttribute), false);
45    }
46
47    /// <summary>
48    /// An optional name for this member that will be used during serialization.
49    /// This allows to rename a field/property in code but still be able to read
50    /// the old serialized format.
51    /// </summary>
52    /// <value>The name.</value>
53    public string Name { get; set; }
54
55
56    /// <summary>
57    /// A default value in case the field/property was not present or not serialized
58    /// in a previous version of the class and could therefore be absent during
59    /// deserialization.
60    /// </summary>
61    /// <value>The default value.</value>
62    public object DefaultValue { get; set; }
63
64    /// <summary>
65    /// Allow storable attribute on properties with only a getter or a setter. These
66    /// properties will then by either only serialized (getter only) but not deserialized or only
67    /// deserialized (setter only) but not serialized again.
68    /// </summary>
69    public bool AllowOneWay { get; set; }
70
71    /// <summary>
72    /// Returns a <see cref="System.String"/> that represents this instance.
73    /// </summary>
74    /// <returns>
75    /// A <see cref="System.String"/> that represents this instance.
76    /// </returns>
77    public override string ToString() {
78      StringBuilder sb = new StringBuilder();
79      sb.Append("[Storable");
80      if (Name != null || DefaultValue != null)
81        sb.Append('(');
82      if (Name != null) {
83        sb.Append("Name = \"").Append(Name).Append("\"");
84        if (DefaultValue != null)
85          sb.Append(", ");
86      }
87      if (DefaultValue != null)
88        sb.Append("DefaultValue = \"").Append(DefaultValue).Append("\"");
89      if (Name != null || DefaultValue != null)
90        sb.Append(')');
91      sb.Append(']');
92      return sb.ToString();
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.