Changeset 15020 for branches/PersistenceReintegration
- Timestamp:
- 06/01/17 15:15:25 (8 years ago)
- Location:
- branches/PersistenceReintegration
- Files:
-
- 6 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 } -
branches/PersistenceReintegration/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCasesPersistenceNew.cs
r15018 r15020 268 268 } 269 269 270 [StorableType("6494ee1d-877b-4e78-83df-f3f55a77b9b4")]271 270 public class NonSerializable { 272 271 int x = 0; … … 1382 1381 int value; 1383 1382 int PropertyValue { get; set; } 1383 1384 /* 1385 [StorableConstructor] 1386 public TestStruct(StorableConstructorFlag deserializing) { 1387 value = 0; 1388 PropertyValue = 0; 1389 } 1390 */ 1391 1384 1392 public TestStruct(int value) 1385 1393 : this() { … … 1584 1592 public int Value4 { get; private set; } 1585 1593 [StorableConstructor] 1586 public AllFieldsStorable(bool isDeserializing) { 1587 if (!isDeserializing) { 1588 Value1 = 12; 1589 Value2 = 23; 1590 Value3 = 34; 1591 Value4 = 56; 1592 } 1594 public AllFieldsStorable(StorableConstructorFlag isdeserializing) { 1595 } 1596 1597 public void InitValues() { 1598 Value1 = 12; 1599 Value2 = 23; 1600 Value3 = 34; 1601 Value4 = 56; 1593 1602 } 1594 1603 } … … 1599 1608 public void TestStorableTypeDiscoveryAllFields() { 1600 1609 ProtoBufSerializer serializer = new ProtoBufSerializer(); 1601 AllFieldsStorable afs = new AllFieldsStorable(false); 1610 AllFieldsStorable afs = new AllFieldsStorable(default(StorableConstructorFlag)); 1611 afs.InitValues(); 1602 1612 serializer.Serialize(afs, tempFile); 1603 1613 AllFieldsStorable newAfs = (AllFieldsStorable)serializer.Deserialize(tempFile); … … 1615 1625 public int Value3 { get; private set; } 1616 1626 public int Value4 { get; private set; } 1617 [StorableConstructor] 1618 public AllPropertiesStorable(bool isDeserializing) { 1619 if (!isDeserializing) { 1620 Value1 = 12; 1621 Value2 = 23; 1622 Value3 = 34; 1623 Value4 = 56; 1624 } 1627 1628 public AllPropertiesStorable() { 1629 } 1630 1631 public void InitValues() { 1632 Value1 = 12; 1633 Value2 = 23; 1634 Value3 = 34; 1635 Value4 = 56; 1625 1636 } 1626 1637 } … … 1631 1642 public void TestStorableTypeDiscoveryAllProperties() { 1632 1643 ProtoBufSerializer serializer = new ProtoBufSerializer(); 1633 AllPropertiesStorable afs = new AllPropertiesStorable(false); 1644 AllPropertiesStorable afs = new AllPropertiesStorable(); 1645 afs.InitValues(); 1634 1646 serializer.Serialize(afs, tempFile); 1635 1647 AllPropertiesStorable newAfs = (AllPropertiesStorable)serializer.Deserialize(tempFile); … … 1648 1660 public int Value3 { get; private set; } 1649 1661 public int Value4 { get; private set; } 1650 [StorableConstructor] 1651 public AllFieldsAndAllPropertiesStorable(bool isDeserializing) { 1652 if (!isDeserializing) { 1653 Value1 = 12; 1654 Value2 = 23; 1655 Value3 = 34; 1656 Value4 = 56; 1657 } 1662 1663 public AllFieldsAndAllPropertiesStorable() { 1664 } 1665 1666 public void InitValues() { 1667 Value1 = 12; 1668 Value2 = 23; 1669 Value3 = 34; 1670 Value4 = 56; 1671 1658 1672 } 1659 1673 } … … 1664 1678 public void TestStorableTypeDiscoveryAllFieldsAndAllProperties() { 1665 1679 ProtoBufSerializer serializer = new ProtoBufSerializer(); 1666 AllFieldsAndAllPropertiesStorable afs = new AllFieldsAndAllPropertiesStorable(false); 1680 AllFieldsAndAllPropertiesStorable afs = new AllFieldsAndAllPropertiesStorable(); 1681 afs.InitValues(); 1667 1682 serializer.Serialize(afs, tempFile); 1668 1683 AllFieldsAndAllPropertiesStorable newAfs = (AllFieldsAndAllPropertiesStorable)serializer.Deserialize(tempFile); … … 1680 1695 public int Value3 { get; private set; } 1681 1696 public int Value4 { get; private set; } 1682 [StorableConstructor]1683 public MarkedOnlyStorable(bool isDeserializing) {1684 if (!isDeserializing) { 1685 Value1 = 12;1686 Value2 = 23;1687 Value3 = 34;1688 Value4 = 56;1689 }1697 public MarkedOnlyStorable() { 1698 } 1699 1700 public void InitValues() { 1701 Value1 = 12; 1702 Value2 = 23; 1703 Value3 = 34; 1704 Value4 = 56; 1690 1705 } 1691 1706 } … … 1696 1711 public void TestStorableTypeDiscoveryMarkedOnly() { 1697 1712 ProtoBufSerializer serializer = new ProtoBufSerializer(); 1698 MarkedOnlyStorable afs = new MarkedOnlyStorable(false); 1713 MarkedOnlyStorable afs = new MarkedOnlyStorable(); 1714 afs.InitValues(); 1699 1715 serializer.Serialize(afs, tempFile); 1700 1716 MarkedOnlyStorable newAfs = (MarkedOnlyStorable)serializer.Deserialize(tempFile);
Note: See TracChangeset
for help on using the changeset viewer.