- Timestamp:
- 05/17/11 14:41:30 (13 years ago)
- Location:
- branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableClassAnalyzer.cs
r6214 r6222 32 32 /// Analyzes storables classes and creates a datastructure containing all required 33 33 /// reflection information to serialize and deserialized these types. 34 /// </summary> 35 [StorableClass] 36 public sealed class StorableClassAnalyzer { 34 /// </summary> 35 public static class StorableClassAnalyzer { 37 36 38 [Storable]39 private static Dictionary<Type, TypeDescriptor> typeDescriptors;37 private static Dictionary<Type, TypeDescriptor> typeDescriptors = new Dictionary<Type, TypeDescriptor>(); 38 private static object typeDescriptorLock = new object(); 40 39 41 40 /// <summary> 42 41 /// A list of all types that have been analyzed by this instance. 43 42 /// </summary> 44 public IEnumerable<Type> KnownTypes { get { return typeDescriptors.Keys; } } 45 46 /// <summary> 47 /// Creates a new instance of the StorableClassAnalyzer. 48 /// </summary> 49 public StorableClassAnalyzer() { 50 typeDescriptors = new Dictionary<Type, TypeDescriptor>(); 51 } 43 public static IEnumerable<Type> KnownTypes { get { lock (typeDescriptorLock) { return typeDescriptors.Keys; } } } 52 44 53 45 /// <summary> … … 58 50 /// <param name="type">The type to be analyzed.</param> 59 51 /// <returns>A TypeDescriptor object that encapsulates all necessary reflection information.</returns> 60 public TypeDescriptor this[Type type]{61 get {62 TypeDescriptor typeDescriptor = null;52 public static TypeDescriptor GetDescriptor(Type type) { 53 TypeDescriptor typeDescriptor = null; 54 lock (typeDescriptorLock) { 63 55 typeDescriptors.TryGetValue(type, out typeDescriptor); 64 if (typeDescriptor != null) 65 return typeDescriptor; 66 typeDescriptor = new TypeDescriptor(type); 67 AddMembers(type, typeDescriptor); 68 if (!typeDescriptor.IsInvalid) { 69 typeDescriptor.Disentangle(); 56 } 57 if (typeDescriptor != null) 58 return typeDescriptor; 59 typeDescriptor = new TypeDescriptor(type); 60 AddMembers(type, typeDescriptor); 61 if (!typeDescriptor.IsInvalid) { 62 typeDescriptor.Disentangle(); 63 lock (typeDescriptorLock) { 70 64 typeDescriptors[type] = typeDescriptor; 71 65 } 72 return typeDescriptor;73 66 } 67 return typeDescriptor; 74 68 } 75 69 76 private void AddMembers(Type type, TypeDescriptor typeDescriptor) {70 private static void AddMembers(Type type, TypeDescriptor typeDescriptor) { 77 71 if (type.BaseType != null) { 78 typeDescriptor.BaseTypeDescriptor = this[type.BaseType];72 typeDescriptor.BaseTypeDescriptor = StorableClassAnalyzer.GetDescriptor(type.BaseType); 79 73 if (typeDescriptor.BaseTypeDescriptor.IsInvalid) { 80 74 typeDescriptor.IsInvalid = true; -
branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs
r6214 r6222 41 41 public sealed class StorableSerializer : ICompositeSerializer { 42 42 43 private StorableClassAnalyzer analyzer; 44 45 public StorableSerializer() { 46 analyzer = new StorableClassAnalyzer(); 47 } 43 public StorableSerializer() { } 48 44 49 45 [StorableConstructor] … … 68 64 /// </returns> 69 65 public bool CanSerialize(Type type) { 70 TypeDescriptor desc = analyzer[type];66 TypeDescriptor desc = StorableClassAnalyzer.GetDescriptor(type); 71 67 if (desc.IsInvalid) 72 68 return false; … … 91 87 public string JustifyRejection(Type type) { 92 88 StringBuilder sb = new StringBuilder(); 93 TypeDescriptor desc = analyzer[type];89 TypeDescriptor desc = StorableClassAnalyzer.GetDescriptor(type); 94 90 if (desc.IsInvalid) 95 91 sb.Append("class is not marked [StorableClass] but has mutable members."); … … 120 116 /// <returns>An enumerable of <see cref="Tag"/>s.</returns> 121 117 public IEnumerable<Tag> Decompose(object obj) { 122 foreach (var kvp in analyzer[obj.GetType()].Decompose(obj)) {118 foreach (var kvp in StorableClassAnalyzer.GetDescriptor(obj.GetType()).Decompose(obj)) { 123 119 yield return new Tag(kvp.Key, kvp.Value); 124 120 } … … 133 129 public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) { 134 130 try { 135 return analyzer[type].CreateInstance();131 return StorableClassAnalyzer.GetDescriptor(type).CreateInstance(); 136 132 } catch (TargetInvocationException x) { 137 133 throw new PersistenceException( … … 148 144 /// <param name="type">The type.</param> 149 145 public void Populate(object instance, IEnumerable<Tag> objects, Type type) { 150 analyzer[type].Populate(instance, objects.ToDictionary(kvp => kvp.Name, kvp => kvp.Value));146 StorableClassAnalyzer.GetDescriptor(type).Populate(instance, objects.ToDictionary(kvp => kvp.Name, kvp => kvp.Value)); 151 147 } 152 148
Note: See TracChangeset
for help on using the changeset viewer.