Changeset 2993 for trunk/sources
- Timestamp:
- 03/10/10 16:37:52 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3
- Files:
-
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs
r2737 r2993 137 137 if (compositeSerializer != null) 138 138 return CompositeEnumerator(accessor.Name, compositeSerializer.Decompose(value), id, typeId, compositeSerializer.CreateMetaInfo(value)); 139 throw new PersistenceException( 140 String.Format( 141 "No suitable method for serializing values of type \"{0}\" found\r\n" + 142 "primitive serializers:\r\n{1}\r\n" + 143 "composite serializers:\r\n{2}", 144 value.GetType().VersionInvariantName(), 145 string.Join("\r\n", configuration.PrimitiveSerializers.Select(f => f.GetType().VersionInvariantName()).ToArray()), 146 string.Join("\r\n", configuration.CompositeSerializers.Select(d => d.GetType().VersionInvariantName()).ToArray()) 147 )); 139 throw CreatePersistenceException(type); 148 140 } catch (Exception x) { 149 141 if (isTestRun) { … … 154 146 } 155 147 } 148 } 149 150 private PersistenceException CreatePersistenceException(Type type) { 151 StringBuilder sb = new StringBuilder(); 152 sb.Append("Could not determine how to serialize a value of type \"") 153 .Append(type.VersionInvariantName()) 154 .AppendLine("\""); 155 sb.AppendLine("No registered primitive serializer for this type:"); 156 foreach (var ps in configuration.PrimitiveSerializers) 157 sb.Append(ps.SourceType.VersionInvariantName()) 158 .Append(" ---- (") 159 .Append(ps.GetType().VersionInvariantName()) 160 .AppendLine(")"); 161 sb.AppendLine("Rejected by all composite serializers:"); 162 foreach (var cs in configuration.CompositeSerializers) 163 sb.Append("\"") 164 .Append(cs.JustifyRejection(type)) 165 .Append("\" ---- (") 166 .Append(cs.GetType().VersionInvariantName()) 167 .AppendLine(")"); 168 return new PersistenceException(sb.ToString()); 156 169 } 157 170 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/ArraySerializer.cs
r1823 r2993 4 4 using System.Collections.Generic; 5 5 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 6 using System.Text; 6 7 7 8 namespace HeuristicLab.Persistence.Default.CompositeSerializers { … … 16 17 public bool CanSerialize(Type type) { 17 18 return type.IsArray || type == typeof(Array); 19 } 20 21 public string JustifyRejection(Type type) { 22 return "not an array and not of type System.Array"; 18 23 } 19 24 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/CompactNumberArray2StringSerializer.cs
r1823 r2993 24 24 (type.IsArray || type == typeof(Array)) && 25 25 numberConverter.CanSerialize(type.GetElementType()); 26 } 27 28 public string JustifyRejection(Type type) { 29 if (!type.IsArray && type != typeof(Array)) 30 return "not an array"; 31 return string.Format("number converter cannot serialize elements: " + 32 numberConverter.JustifyRejection(type.GetElementType())); 26 33 } 27 34 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/DictionarySerializer.cs
r1823 r2993 20 20 return ReflectionTools.HasDefaultConstructor(type) && 21 21 type.GetInterface(typeof(IDictionary).FullName) != null; 22 } 23 24 public string JustifyRejection(Type type) { 25 if (!ReflectionTools.HasDefaultConstructor(type)) 26 return "no default constructor"; 27 return "interface IDictionary not implemented"; 22 28 } 23 29 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/EnumSerializer.cs
r1823 r2993 16 16 public bool CanSerialize(Type type) { 17 17 return type.IsEnum || type == typeof(Enum); 18 } 19 20 public string JustifyRejection(Type type) { 21 return "not an enum and not System.Enum"; 18 22 } 19 23 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/EnumerableSerializer.cs
r1823 r2993 26 26 } 27 27 28 public string JustifyRejection(Type type) { 29 if (!ReflectionTools.HasDefaultConstructor(type)) 30 return "no default constructor"; 31 if (type.GetInterface(typeof(IEnumerable).FullName) == null) 32 return "interface IEnumerable not implemented"; 33 if (type.GetMethod("Add") == null) 34 return "no 'Add()' method"; 35 return "no 'Add()' method with one argument"; 36 } 37 28 38 public IEnumerable<Tag> CreateMetaInfo(object o) { 29 39 return new Tag[] { }; -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/KeyValuePairSerializer.cs
r1823 r2993 16 16 } 17 17 18 private static readonly Type genericKeyValuePairType = 19 typeof(KeyValuePair<int, int>).GetGenericTypeDefinition(); 18 20 19 21 public bool CanSerialize(Type type) { 20 22 return type.IsGenericType && 21 type.GetGenericTypeDefinition() == 22 typeof(KeyValuePair<int, int>).GetGenericTypeDefinition(); 23 type.GetGenericTypeDefinition() == genericKeyValuePairType; 24 } 25 26 public string JustifyRejection(Type type) { 27 if (!type.IsGenericType) 28 return "not even generic"; 29 return "not generic KeyValuePair<,>"; 23 30 } 24 31 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Number2StringSerializer.cs
r1853 r2993 1 1 using System; 2 using System.Linq; 2 3 using HeuristicLab.Persistence.Interfaces; 3 4 using HeuristicLab.Persistence.Core; … … 43 44 public bool CanSerialize(Type type) { 44 45 return numberParsers.ContainsKey(type); 46 } 47 48 public string JustifyRejection(Type type) { 49 return string.Format("not a number type (one of {0})", 50 string.Join(", ", numberTypes.Select(n => n.Name).ToArray())); 45 51 } 46 52 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/NumberEnumerable2StringSerializer.cs
r1823 r2993 60 60 } 61 61 62 public string JustifyRejection(Type type) { 63 if (!ReflectionTools.HasDefaultConstructor(type)) 64 return "no default constructor"; 65 if (!ImplementsGenericEnumerable(type)) 66 return "IEnumerable<> not implemented"; 67 return "no Add method with one parameter"; 68 } 69 62 70 public IEnumerable<Tag> CreateMetaInfo(object o) { 63 71 return new Tag[] { }; -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/StackSerializer.cs
r1823 r2993 22 22 type.IsGenericType && 23 23 type.GetGenericTypeDefinition() == typeof(Stack<>); 24 } 25 26 public string JustifyRejection(Type type) { 27 return "not Stack or generic Stack<>"; 24 28 } 25 29 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs
r2991 r2993 17 17 18 18 public bool CanSerialize(Type type) { 19 if (!ReflectionTools.HasDefaultConstructor(type)) 19 if (!ReflectionTools.HasDefaultConstructor(type) && 20 StorableConstructorAttribute.GetStorableConstructor(type) == null) 20 21 return false; 21 22 while (type != null) { … … 26 27 } 27 28 return true; 29 } 30 31 public string JustifyRejection(Type type) { 32 if (!ReflectionTools.HasDefaultConstructor(type) && 33 StorableConstructorAttribute.GetStorableConstructor(type) == null) 34 return "no default constructor and no storable constructor"; 35 while (type != null) { 36 if (StorableAttribute.GetStorableMembers(type, false).Count() == 0 && 37 !EmptyStorableClassAttribute.IsEmptyStorable(type)) 38 return string.Format("{0} has no storable members and is not marked [EmtpyStorableClass]", 39 type); 40 type = type.BaseType; 41 } 42 return "no reason"; 28 43 } 29 44 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/StructSerializer.cs
r2939 r2993 19 19 public bool CanSerialize(Type type) { 20 20 return type.IsValueType && !type.IsPrimitive && !type.IsEnum && type.IsSealed; 21 } 22 23 public string JustifyRejection(Type type) { 24 if (!type.IsValueType) 25 return "not a value type"; 26 if (type.IsPrimitive) 27 return "type is primitive (int, float, ...)"; 28 if (type.IsEnum) 29 return "type is enum"; 30 return "type is not sealed"; 21 31 } 22 32 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/TypeSerializer.cs
r1843 r2993 18 18 return type == typeof(Type) || 19 19 type.VersionInvariantName() == "System.RuntimeType, mscorlib"; 20 } 21 22 public string JustifyRejection(Type type) { 23 return "not System.Type nor System.RuntimeType"; 20 24 } 21 25 -
trunk/sources/HeuristicLab.Persistence/3.3/Interfaces/ICompositeSerializer.cs
r1823 r2993 20 20 /// </summary> 21 21 bool CanSerialize(Type type); 22 23 /// <summary> 24 /// Give a reason if possibly why the given type cannot be serialized by this 25 /// ICompositeSerializer. 26 /// </summary> 27 string JustifyRejection(Type type); 22 28 23 29 /// <summary> -
trunk/sources/HeuristicLab.Persistence/3.3/Tests/UseCases.cs
r2990 r2993 736 736 } 737 737 738 [TestMethod] 739 public void TestRejectionJustifications() { 740 NonSerializable ns = new NonSerializable(); 741 try { 742 XmlGenerator.Serialize(ns, tempFile); 743 Assert.Fail("PersistenceException expected"); 744 } catch (PersistenceException x) { 745 Assert.IsTrue(x.Message.Contains(new StorableSerializer().JustifyRejection(typeof(NonSerializable)))); 746 } 747 } 748 738 749 [ClassInitialize] 739 750 public static void Initialize(TestContext testContext) {
Note: See TracChangeset
for help on using the changeset viewer.