Changeset 16210 for branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StorableAttribute.cs
- Timestamp:
- 10/03/18 17:48:20 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StorableAttribute.cs
r15035 r16210 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Reflection; 24 using System.Text;25 25 26 26 namespace HeuristicLab.Persistence { 27 28 29 27 /// <summary> 30 28 /// Mark the member of a class to be considered by the <c>StorableSerializer</c>. … … 36 34 AttributeTargets.Field | AttributeTargets.Property, 37 35 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 } 36 Inherited = false 37 )] 38 public sealed class StorableAttribute : Attribute { 39 private static IDictionary<MemberInfo, StorableAttribute> attributeCache = new Dictionary<MemberInfo, StorableAttribute>(); 46 40 41 #region Properties 47 42 /// <summary> 48 43 /// An optional name for this member that will be used during serialization. … … 67 62 /// deserialized (setter only) but not serialized again. 68 63 /// </summary> 64 [Obsolete("Use OldName instead.")] 69 65 public bool AllowOneWay { get; set; } 70 66 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(", "); 67 public string OldName { get; set; } 68 #endregion 69 70 public static bool IsStorable(MemberInfo memberInfo) { 71 return GetStorableAttribute(memberInfo) != null; 72 } 73 public static StorableAttribute GetStorableAttribute(MemberInfo memberInfo) { 74 StorableAttribute attrib; 75 76 if (!attributeCache.TryGetValue(memberInfo, out attrib)) { 77 attrib = (StorableAttribute)GetCustomAttribute(memberInfo, typeof(StorableAttribute), false); 78 if (attrib != null) attributeCache[memberInfo] = attrib; 86 79 } 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(); 80 81 return attrib; 93 82 } 94 83 }
Note: See TracChangeset
for help on using the changeset viewer.