Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/15/17 13:26:49 (6 years ago)
Author:
jkarder
Message:

#2520: worked on new persistence

  • changed message definitions
  • updated transformers
  • worked on conversions
  • cleaned up
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/TypeInfo.cs

    r15034 r15529  
    5858      StorableTypeAttribute = StorableTypeAttribute.GetStorableTypeAttribute(type);
    5959      if (StorableTypeAttribute != null) {
    60         // check constructors (
     60        string guidPrefix = StorableTypeAttribute.Guid.ToString().ToUpper();
     61        // check constructors
    6162        if (!type.IsValueType && !type.IsEnum && !type.IsInterface &&
    6263          GetStorableConstructor() == null && GetDefaultConstructor() == null)
    6364          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>();
    67         while (type != null) {
    68           types.Push(type);
    69           type = type.BaseType;
    70         }
    7165
    7266        var fields = new List<ComponentInfo>();
     
    7468        var beforeSerializationHooks = new List<MethodInfo>();
    7569        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 fields
    81             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           }
    8970
    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
    9974
    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();
    10477
    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));
    11582          }
    11683        }
     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
    117115        Fields = fields;
    118116        Properties = properties;
     
    155153
    156154    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();
    161161    }
    162162
    163163    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);
    166165    }
    167166  }
Note: See TracChangeset for help on using the changeset viewer.