Changeset 3036 for trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Number2StringSerializer.cs
- Timestamp:
- 03/15/10 14:45:46 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Number2StringSerializer.cs
r3017 r3036 12 12 namespace HeuristicLab.Persistence.Default.CompositeSerializers { 13 13 14 /// <summary> 15 /// Serializes a primitive number type using the ToString() method and an 16 /// approriate precision and parses back the generated string using 17 /// the number type's Parse() method. 18 /// 19 /// This serializer has Priorty below zero and is disabled by default 20 /// but can be useful in generating custom serializers. 21 /// </summary> 14 22 [StorableClass] 15 public class Number2StringSerializer : ICompositeSerializer {23 public sealed class Number2StringSerializer : ICompositeSerializer { 16 24 17 25 private static readonly List<Type> numberTypes = … … 42 50 } 43 51 52 /// <summary> 53 /// Determines for every type whether the composite serializer is applicable. 54 /// </summary> 55 /// <param name="type">The type.</param> 56 /// <returns> 57 /// <c>true</c> if this instance can serialize the specified type; otherwise, <c>false</c>. 58 /// </returns> 44 59 public bool CanSerialize(Type type) { 45 60 return numberParsers.ContainsKey(type); 46 61 } 47 62 63 /// <summary> 64 /// Give a reason if possibly why the given type cannot be serialized by this 65 /// ICompositeSerializer. 66 /// </summary> 67 /// <param name="type">The type.</param> 68 /// <returns> 69 /// A string justifying why type cannot be serialized. 70 /// </returns> 48 71 public string JustifyRejection(Type type) { 49 72 return string.Format("not a number type (one of {0})", … … 51 74 } 52 75 76 /// <summary> 77 /// Formats the specified obj. 78 /// </summary> 79 /// <param name="obj">The obj.</param> 80 /// <returns></returns> 53 81 public string Format(object obj) { 54 82 if (obj.GetType() == typeof(float)) … … 61 89 } 62 90 91 /// <summary> 92 /// Parses the specified string value. 93 /// </summary> 94 /// <param name="stringValue">The string value.</param> 95 /// <param name="type">The type.</param> 96 /// <returns></returns> 63 97 public object Parse(string stringValue, Type type) { 64 98 try { … … 75 109 76 110 111 112 /// <summary> 113 /// Defines the Priorty of this composite serializer. Higher number means 114 /// higher prioriy. Negative numbers are fallback serializers that are 115 /// disabled by default. 116 /// All default generic composite serializers have priority 100. Specializations 117 /// have priority 200 so they will be tried first. Priorities are 118 /// only considered for default configurations. 119 /// </summary> 120 /// <value></value> 77 121 public int Priority { 78 122 get { return -100; } 79 123 } 80 124 125 /// <summary> 126 /// Generate MetaInfo necessary for instance creation. (e.g. dimensions 127 /// necessary for array creation. 128 /// </summary> 129 /// <param name="obj">An object.</param> 130 /// <returns>An enumerable of <see cref="Tag"/>s.</returns> 81 131 public IEnumerable<Tag> CreateMetaInfo(object obj) { 82 132 yield return new Tag(Format(obj)); 83 133 } 84 134 135 /// <summary> 136 /// Decompose an object into <see cref="Tag"/>s, the tag name can be null, 137 /// the order in which elements are generated is guaranteed to be 138 /// the same as they will be supplied to the Populate method. 139 /// </summary> 140 /// <param name="obj">An object.</param> 141 /// <returns>An enumerable of <see cref="Tag"/>s.</returns> 85 142 public IEnumerable<Tag> Decompose(object obj) { 86 143 // numbers are composed just of meta info … … 88 145 } 89 146 147 /// <summary> 148 /// Create an instance of the object using the provided meta information. 149 /// </summary> 150 /// <param name="type">A type.</param> 151 /// <param name="metaInfo">The meta information.</param> 152 /// <returns>A fresh instance of the provided type.</returns> 90 153 public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) { 91 154 var it = metaInfo.GetEnumerator(); … … 102 165 } 103 166 167 /// <summary> 168 /// Fills an object with values from the previously generated <see cref="Tag"/>s 169 /// in Decompose. The order in which the values are supplied is 170 /// the same as they where generated. <see cref="Tag"/> names might be null. 171 /// </summary> 172 /// <param name="instance">An empty object instance.</param> 173 /// <param name="tags">The tags.</param> 174 /// <param name="type">The type.</param> 104 175 public void Populate(object instance, IEnumerable<Tag> tags, Type type) { 105 176 // numbers are composed just of meta info, no need to populate
Note: See TracChangeset
for help on using the changeset viewer.