- Timestamp:
- 12/15/17 13:26:49 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/TypeInfo.cs
r15034 r15529 58 58 StorableTypeAttribute = StorableTypeAttribute.GetStorableTypeAttribute(type); 59 59 if (StorableTypeAttribute != null) { 60 // check constructors ( 60 string guidPrefix = StorableTypeAttribute.Guid.ToString().ToUpper(); 61 // check constructors 61 62 if (!type.IsValueType && !type.IsEnum && !type.IsInterface && 62 63 GetStorableConstructor() == null && GetDefaultConstructor() == null) 63 64 throw new PersistenceException("No storable constructor or parameterless constructor found."); 64 65 // traverse type hierarchy from base type to sub types66 Stack<Type> types = new Stack<Type>();67 while (type != null) {68 types.Push(type);69 type = type.BaseType;70 }71 65 72 66 var fields = new List<ComponentInfo>(); … … 74 68 var beforeSerializationHooks = new List<MethodInfo>(); 75 69 var afterDeserializationHooks = new List<MethodInfo>(); 76 while (types.Count > 0) {77 type = types.Pop();78 if (StorableTypeAttribute.MemberSelection != StorableMemberSelection.AllProperties) {79 var fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic)80 .Where(x => !x.Name.StartsWith("<") && !x.Name.EndsWith("k__BackingField")); // exclude backing fields81 if (StorableTypeAttribute.MemberSelection == StorableMemberSelection.MarkedOnly)82 fieldInfos = fieldInfos.Where(x => StorableAttribute.IsStorable(x)).ToArray();83 foreach (var field in fieldInfos) {84 var attrib = StorableAttribute.GetStorableAttribute(field);85 var name = attrib == null || string.IsNullOrEmpty(attrib.Name) ? field.Name : attrib.Name;86 fields.Add(new ComponentInfo(type.Name + '.' + name, field, attrib, true, true));87 }88 }89 70 90 if (StorableTypeAttribute.MemberSelection != StorableMemberSelection.AllFields) { 91 var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic). 92 Where(x => x.GetIndexParameters().Length == 0); // exclude indexed properties 93 if (StorableTypeAttribute.MemberSelection == StorableMemberSelection.MarkedOnly) 94 propertyInfos = propertyInfos.Where(x => StorableAttribute.IsStorable(x)).ToArray(); 95 foreach (var property in propertyInfos) { 96 var attrib = StorableAttribute.GetStorableAttribute(property); 97 if ((!property.CanRead || !property.CanWrite) && (attrib == null || !attrib.AllowOneWay)) 98 throw new PersistenceException("Properties must be readable and writable or explicity enable one way serialization."); 71 if (StorableTypeAttribute.MemberSelection != StorableMemberSelection.AllProperties) { 72 var fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic) 73 .Where(x => !x.Name.StartsWith("<") && !x.Name.EndsWith("k__BackingField")); // exclude backing fields 99 74 100 var name = attrib == null || string.IsNullOrEmpty(attrib.Name) ? property.Name : attrib.Name; 101 properties.Add(new ComponentInfo(type.Name + '.' + name, property, attrib, property.CanRead, property.CanWrite)); 102 } 103 } 75 if (StorableTypeAttribute.MemberSelection == StorableMemberSelection.MarkedOnly) 76 fieldInfos = fieldInfos.Where(StorableAttribute.IsStorable).ToArray(); 104 77 105 var methodInfos = type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic). 106 Where(x => StorableHookAttribute.IsStorableHook(x)). 107 Where(x => (x.ReturnType == typeof(void)) && (x.GetParameters().Length == 0)); 108 foreach (var method in methodInfos) { 109 foreach (var attrib in StorableHookAttribute.GetStorableHookAttributes(method)) { 110 if (attrib.HookType == HookType.BeforeSerialization) 111 beforeSerializationHooks.Add(method); 112 if (attrib.HookType == HookType.AfterDeserialization) 113 afterDeserializationHooks.Add(method); 114 } 78 foreach (var field in fieldInfos) { 79 var attrib = StorableAttribute.GetStorableAttribute(field); 80 var name = attrib == null || string.IsNullOrEmpty(attrib.Name) ? field.Name : attrib.Name; 81 fields.Add(new ComponentInfo(name, guidPrefix + "." + name, field, attrib, true, true)); 115 82 } 116 83 } 84 85 if (StorableTypeAttribute.MemberSelection != StorableMemberSelection.AllFields) { 86 var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic) 87 .Where(x => !x.GetIndexParameters().Any()); // exclude indexed properties 88 89 if (StorableTypeAttribute.MemberSelection == StorableMemberSelection.MarkedOnly) 90 propertyInfos = propertyInfos.Where(StorableAttribute.IsStorable).ToArray(); 91 92 foreach (var property in propertyInfos) { 93 var attrib = StorableAttribute.GetStorableAttribute(property); 94 if ((!property.CanRead || !property.CanWrite) && (attrib == null || !attrib.AllowOneWay)) 95 throw new PersistenceException("Properties must be readable and writable or have one way serialization explicitly enabled."); 96 97 var name = attrib == null || string.IsNullOrEmpty(attrib.Name) ? property.Name : attrib.Name; 98 properties.Add(new ComponentInfo(name, guidPrefix + "." + name, property, attrib, property.CanRead, property.CanWrite)); 99 } 100 } 101 102 var methodInfos = type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic) 103 .Where(StorableHookAttribute.IsStorableHook) 104 .Where(x => x.ReturnType == typeof(void) && !x.GetParameters().Any()); 105 106 foreach (var method in methodInfos) { 107 foreach (var attrib in StorableHookAttribute.GetStorableHookAttributes(method)) { 108 if (attrib.HookType == HookType.BeforeSerialization) 109 beforeSerializationHooks.Add(method); 110 if (attrib.HookType == HookType.AfterDeserialization) 111 afterDeserializationHooks.Add(method); 112 } 113 } 114 117 115 Fields = fields; 118 116 Properties = properties; … … 155 153 156 154 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(); 155 return (from ctor in Type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) 156 let parameters = ctor.GetParameters() 157 where StorableConstructorAttribute.IsStorableConstructor(ctor) 158 && parameters.Length == 1 159 && parameters[0].ParameterType == typeof(StorableConstructorFlag) 160 select ctor).FirstOrDefault(); 161 161 } 162 162 163 163 private ConstructorInfo GetDefaultConstructor() { 164 return Type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, 165 null, Type.EmptyTypes, null); 164 return Type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); 166 165 } 167 166 }
Note: See TracChangeset
for help on using the changeset viewer.