- Timestamp:
- 06/01/17 15:15:25 (8 years ago)
- Location:
- branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/Mapper.cs
r14771 r15020 134 134 135 135 public object CreateInstance(Type type) { 136 return StaticCache.GetTypeInfo(type).GetConstructor()(); 136 try { 137 return StaticCache.GetTypeInfo(type).GetConstructor()(); 138 } catch (Exception e) { 139 throw new PersistenceException("Deserialization failed.", e); 140 } 137 141 } 138 142 -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/Serializer.cs
r13347 r15020 20 20 #endregion 21 21 22 using System; 22 23 using System.IO; 23 24 using System.IO.Compression; … … 29 30 } 30 31 public virtual void Serialize(object o, string path) { 31 using (var fileStream = new FileStream(path, FileMode.Create)) { 32 using (var zipStream = new DeflateStream(fileStream, CompressionMode.Compress)) { 32 string tempfile = Path.GetTempFileName(); 33 34 using (FileStream stream = File.Create(tempfile)) { 35 using (var zipStream = new DeflateStream(stream, CompressionMode.Compress)) { 33 36 Serialize(o, zipStream); 34 37 } 35 38 } 39 40 File.Copy(tempfile, path, true); 41 File.Delete(tempfile); 36 42 } 37 43 public virtual byte[] Serialize(object o) { -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StaticCache.cs
r14927 r15020 244 244 TypeInfo typeInfo; 245 245 if (!typeInfos.TryGetValue(type, out typeInfo)) { 246 var transformer = guid2Transformer.Values.OrderBy(x => x.Priority).First(x => x.CanTransformType(type)); 246 var transformer = guid2Transformer.Values.OrderBy(x => x.Priority).FirstOrDefault(x => x.CanTransformType(type)); 247 if(transformer == null) throw new PersistenceException("No transformer found for type " + type.AssemblyQualifiedName); 247 248 typeInfo = new TypeInfo(type, transformer); 248 249 typeInfos.Add(type, typeInfo); -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StorableConstructorFlag.cs
r15018 r15020 25 25 26 26 namespace HeuristicLab.Persistence { 27 // the enumtype which is used for the definition of storable constructors28 public struct StorableConstructorFlag {27 // the type which is used for the definition of storable constructors 28 public struct StorableConstructorFlag { 29 29 // empty 30 30 }; -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/TypeInfo.cs
r15018 r15020 58 58 StorableTypeAttribute = StorableTypeAttribute.GetStorableTypeAttribute(type); 59 59 if (StorableTypeAttribute != null) { 60 // traverse type hierarchy from base type to sub types 61 Stack<Type> types = new Stack<Type>(); 60 // check constructors ( 61 if (!type.IsValueType && !type.IsEnum && !type.IsInterface && 62 GetStorableConstructor() == null && GetDefaultConstructor() == null) 63 throw new PersistenceException("No storable constructor or parameterless constructor found."); 64 65 // traverse type hierarchy from base type to sub types 66 Stack<Type> types = new Stack<Type>(); 62 67 while (type != null) { 63 68 types.Push(type); … … 90 95 foreach (var property in propertyInfos) { 91 96 var attrib = StorableAttribute.GetStorableAttribute(property); 97 if (!attrib.AllowOneWay && (!property.CanRead || !property.CanWrite)) 98 throw new PersistenceException("Properties must be readable and writable or explicity enable one way serialization."); 99 92 100 var name = attrib == null || string.IsNullOrEmpty(attrib.Name) ? property.Name : attrib.Name; 93 101 properties.Add(new ComponentInfo(type.Name + '.' + name, property, attrib, property.CanRead, property.CanWrite)); … … 118 126 119 127 // get storable constructor 120 var ctor = Type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) 121 .Where(x => StorableConstructorAttribute.IsStorableConstructor(x)) 122 .Where(x => (x.GetParameters().Length == 1) && (x.GetParameters()[0].ParameterType == typeof(StorableConstructorFlag))) 123 .FirstOrDefault(); 128 var ctor = GetStorableConstructor(); 124 129 if (ctor != null) { 125 130 DynamicMethod dm = new DynamicMethod("", typeof(object), null, Type, true); … … 136 141 137 142 // get default constructor 138 ctor = Type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, 139 null, Type.EmptyTypes, null); 143 ctor = GetDefaultConstructor(); 140 144 if (ctor != null) { 141 145 DynamicMethod dm = new DynamicMethod("", typeof(object), null, Type, true); … … 147 151 } 148 152 149 throw new Exception("No storable constructor or parameterless constructor found."); 153 throw new PersistenceException("No storable constructor or parameterless constructor found."); 154 } 155 156 private ConstructorInfo GetStorableConstructor() { 157 return Type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) 158 .Where(x => StorableConstructorAttribute.IsStorableConstructor(x)) 159 .Where(x => (x.GetParameters().Length == 1) && (x.GetParameters()[0].ParameterType == typeof(StorableConstructorFlag))) 160 .FirstOrDefault(); 161 } 162 163 private ConstructorInfo GetDefaultConstructor() { 164 return Type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, 165 null, Type.EmptyTypes, null); 150 166 } 151 167 }
Note: See TracChangeset
for help on using the changeset viewer.