- Timestamp:
- 06/16/17 11:32:56 (8 years ago)
- Location:
- branches/PersistenceReintegration/HeuristicLab.Persistence/4.0
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StorableConversionAttribute.cs
r14927 r15034 26 26 [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] 27 27 public sealed class StorableConversionAttribute : Attribute { 28 public uint SrcVersion { get; set; } 28 public Guid Guid { get; private set; } 29 public uint SrcVersion { get; private set; } 29 30 30 public StorableConversionAttribute(uint srcVersion) { 31 public StorableConversionAttribute(string guid, uint srcVersion) { 32 this.Guid = new Guid(guid); 31 33 this.SrcVersion = srcVersion; 32 34 } … … 35 37 return Attribute.IsDefined(mi, typeof(StorableConversionAttribute), false); 36 38 } 37 public static StorableConversionAttribute GetTransformerAttribute(MethodInfo mi) { 39 40 public static StorableConversionAttribute GetStorableConversionAttribute(MethodInfo mi) { 38 41 return (StorableConversionAttribute)Attribute.GetCustomAttribute(mi, typeof(StorableConversionAttribute), false); 39 42 } 43 public static Guid GetGuid(MethodInfo mi) { 44 return GetStorableConversionAttribute(mi).Guid; 45 } 40 46 public static uint GetVersion(MethodInfo mi) { 41 return Get TransformerAttribute(mi).SrcVersion;47 return GetStorableConversionAttribute(mi).SrcVersion; 42 48 } 43 49 } -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StorableTypeAttribute.cs
r14925 r15034 37 37 38 38 public Guid Guid { get; private set; } 39 public uint Version { get; set; }39 public uint Version { get; private set; } 40 40 41 41 /// <summary> -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/TypeInfo.cs
r15020 r15034 59 59 if (StorableTypeAttribute != null) { 60 60 // check constructors ( 61 if (!type.IsValueType && !type.IsEnum && !type.IsInterface && 61 if (!type.IsValueType && !type.IsEnum && !type.IsInterface && 62 62 GetStorableConstructor() == null && GetDefaultConstructor() == null) 63 63 throw new PersistenceException("No storable constructor or parameterless constructor found."); 64 64 65 66 65 // traverse type hierarchy from base type to sub types 66 Stack<Type> types = new Stack<Type>(); 67 67 while (type != null) { 68 68 types.Push(type); … … 95 95 foreach (var property in propertyInfos) { 96 96 var attrib = StorableAttribute.GetStorableAttribute(property); 97 if ( !attrib.AllowOneWay && (!property.CanRead || !property.CanWrite))97 if ((!property.CanRead || !property.CanWrite) && (attrib == null || !attrib.AllowOneWay)) 98 98 throw new PersistenceException("Properties must be readable and writable or explicity enable one way serialization."); 99 99 -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Transformers/StorableClassTransformer.cs
r14927 r15034 72 72 var typeBox = mapper.GetBox(box.TypeId).GetExtension(TypeBox.Type); 73 73 var version = typeBox.HasVersion ? typeBox.Version : 1; 74 var typeGuid = typeInfo.StorableTypeAttribute.Guid; 74 75 75 76 var components = new Dictionary<uint, uint>(); … … 79 80 80 81 var conversionMethods = 81 type.GetMethods(BindingFlags.NonPublic | BindingFlags.Static) 82 type.Assembly.GetTypes().SelectMany(t => 83 t.GetMethods(BindingFlags.NonPublic | BindingFlags.Static) 82 84 .Where(StorableConversionAttribute.IsStorableConversionMethod) 83 .Where(mi => StorableConversionAttribute.GetVersion(mi) >= version) 84 .OrderBy(StorableConversionAttribute.GetVersion); 85 .Where(mi => StorableConversionAttribute.GetGuid(mi) == typeGuid && 86 StorableConversionAttribute.GetVersion(mi) >= version)) 87 .OrderBy(StorableConversionAttribute.GetVersion) 88 .ToArray(); 85 89 86 // put all objects into dictionary for optional conversion90 // put all objects into dictionary for conversion 87 91 var dict = new Dictionary<string, object>(); 88 92 foreach (var component in components) { … … 90 94 } 91 95 92 // TODO: check that all entries in the dictionary can be mapped to a field or property93 96 foreach (var convMeth in conversionMethods) { 97 if (StorableConversionAttribute.GetVersion(convMeth) != version) 98 throw new PersistenceException(string.Format("No conversion method defined for type {0} version {1}", typeGuid, version)); 94 99 dict = (Dictionary<string, object>)convMeth.Invoke(null, new object[] { dict }); 100 version++; 101 } 102 if (version != typeInfo.StorableTypeAttribute.Version) 103 throw new PersistenceException(string.Format("Missing one or more conversion methods for type {0} version {1}", 104 typeGuid, typeInfo.StorableTypeAttribute.Version)); 105 106 // set default values for all fields and properties 107 foreach (var componentInfo in typeInfo.Fields) { 108 var field = (FieldInfo)componentInfo.MemberInfo; 109 if (componentInfo.StorableAttribute != null && componentInfo.StorableAttribute.DefaultValue != null) 110 field.SetValue(obj, componentInfo.StorableAttribute.DefaultValue); 111 } 112 foreach (var componentInfo in typeInfo.Properties.Where(x => x.Writeable)) { 113 var property = (PropertyInfo)componentInfo.MemberInfo; 114 if (componentInfo.StorableAttribute != null && componentInfo.StorableAttribute.DefaultValue != null) 115 property.SetValue(obj, componentInfo.StorableAttribute.DefaultValue, null); 95 116 } 96 117 97 foreach (var componentInfo in typeInfo.Fields) { 98 var field = (FieldInfo)componentInfo.MemberInfo; 99 object val = null; 100 bool found = dict.TryGetValue(componentInfo.Name, out val); 101 if (found) 118 // set all members as generated by conversion method chain 119 foreach (var kvp in dict.ToArray()) { 120 var key = kvp.Key; 121 var val = kvp.Value; 122 var fieldInfo = typeInfo.Fields.FirstOrDefault(fi => fi.Name == key); 123 if (fieldInfo != null) { 124 var field = (FieldInfo)fieldInfo.MemberInfo; 102 125 field.SetValue(obj, val); 103 else if (componentInfo.StorableAttribute.DefaultValue != null) 104 field.SetValue(obj, componentInfo.StorableAttribute.DefaultValue); 126 dict.Remove(fieldInfo.Name); 127 continue; 128 } 129 var propInfo = typeInfo.Properties.Where(x => x.Writeable).FirstOrDefault(pi => pi.Name == key); 130 if (propInfo != null) { 131 var prop = (PropertyInfo)propInfo.MemberInfo; 132 prop.SetValue(obj, val, null); 133 dict.Remove(propInfo.Name); 134 continue; 135 } 105 136 } 106 137 107 foreach (var componentInfo in typeInfo.Properties.Where(x => x.Writeable)) { 108 var property = (PropertyInfo)componentInfo.MemberInfo; 109 object val = null; 110 bool found = dict.TryGetValue(componentInfo.Name, out val); 111 if (found) 112 property.SetValue(obj, val, null); 113 else if (componentInfo.StorableAttribute.DefaultValue != null) 114 property.SetValue(obj, componentInfo.StorableAttribute.DefaultValue, null); 115 } 138 if (dict.Any()) 139 throw new PersistenceException(string.Format("Invalid conversion method. The following members are undefined in type {0} version {1}: {2}", 140 typeGuid, typeInfo.StorableTypeAttribute.Version, 141 string.Join(", ", dict.Keys))); 116 142 117 143 var emptyArgs = new object[0]; -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs
r15022 r15034 332 332 return ((Type)mapper.GetObject(b.GetGenericTypeIds(0))).MakeArrayType(); 333 333 } else { 334 334 335 return type; 335 336 }
Note: See TracChangeset
for help on using the changeset viewer.