Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/11/09 16:09:29 (15 years ago)
Author:
epitzer
Message:

Store full version information with serialized data, only fall back to version invariant format if loading fails with full version. Also check, that loaded version is newer than requested version if possible. (#613)

Location:
trunk/sources/HeuristicLab.Persistence/3.3/Core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/DeSerializer.cs

    r1779 r1780  
    99namespace HeuristicLab.Persistence.Core {
    1010
     11  public class TypeLoader {
     12
     13    public static Type Load(string typeNameString) {
     14      Type type;
     15      try {
     16        type = Type.GetType(typeNameString, true);
     17      } catch (Exception) {
     18        Logger.Warn(String.Format(
     19          "Cannot load type \"{0}\", falling back to loading with partial name", typeNameString));
     20        try {
     21          TypeName typeName = TypeNameParser.Parse(typeNameString);
     22          Assembly a = Assembly.LoadWithPartialName(typeName.AssemblyName);
     23          type = a.GetType(typeName.ToString(false, false), true);
     24        } catch (Exception) {
     25          throw new PersistenceException(String.Format(
     26            "Could not load type \"{0}\"",
     27            typeNameString));
     28        }
     29        try {
     30          if (
     31            TypeNameParser.Parse(type.AssemblyQualifiedName).IsOlderThan(
     32            TypeNameParser.Parse(typeNameString)))
     33            throw new PersistenceException(String.Format(
     34              "Serialized type is newer than available type: serialized: {0}, loaded: {1}",
     35              typeNameString,
     36              type.AssemblyQualifiedName));
     37        } catch (PersistenceException) {
     38          throw;
     39        } catch (Exception e) {
     40          Logger.Warn(String.Format(
     41            "Could not perform version check requested type was {0} while loaded type is {1}:",
     42            typeNameString,
     43            type.AssemblyQualifiedName),
     44            e);
     45        }
     46      }
     47      return type;
     48    }
     49
     50  }
     51
    1152  public class Deserializer {
    1253
     
    71112        var map = new Dictionary<Type, object>();
    72113        foreach (var typeMapping in typeCache) {
    73           Type type;
    74           try {
    75             type = Type.GetType(typeMapping.TypeName, true);
    76           } catch (Exception) {
    77             Logger.Error(String.Format(
    78               "Cannot load type \"{0}\", falling back to loading with partial name", typeMapping.TypeName));
    79             string[] typeNameParts = typeMapping.TypeName.Split(new[] { ',' });
    80             try {
    81               Assembly a = Assembly.LoadWithPartialName(typeNameParts[typeNameParts.Length - 1].Trim());
    82               Array.Resize(ref typeNameParts, typeNameParts.Length - 1);
    83               type = a.GetType(string.Join(",", typeNameParts), true);
    84             } catch (Exception) {
    85               throw new PersistenceException(String.Format(
    86                 "Could not load type \"{0}\"",
    87                 typeMapping.TypeName));
    88             }
    89           }
     114          Type type = TypeLoader.Load(typeMapping.TypeName);
    90115          typeIds.Add(typeMapping.Id, type);
    91           Type serializerType = Type.GetType(typeMapping.Serializer, true);
     116          Type serializerType = TypeLoader.Load(typeMapping.Serializer);
    92117          map.Add(type, Activator.CreateInstance(serializerType, true));
    93118        }
     
    101126      }
    102127    }
     128
    103129
    104130    public object Deserialize(IEnumerable<ISerializationToken> tokens) {
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs

    r1710 r1780  
    4040          IFormatter f = configuration.GetFormatter(pair.Key);
    4141          if (f != null) {
    42             serializer = f.GetType().VersionInvariantName();
     42            serializer = f.GetType().AssemblyQualifiedName;
    4343          } else {
    4444            IDecomposer d = configuration.GetDecomposer(pair.Key);
    45             serializer = d.GetType().VersionInvariantName();
     45            serializer = d.GetType().AssemblyQualifiedName;
    4646          }
     47          //result.Add(new TypeMapping(pair.Value, pair.Key.AssemblyQualifiedName, serializer));
    4748          result.Add(new TypeMapping(pair.Value, pair.Key.VersionInvariantName(), serializer));
    4849        }
Note: See TracChangeset for help on using the changeset viewer.