using System.Collections.Generic; using System.Collections; using System; namespace Persistence { public class Serializer : IEnumerable { private readonly object obj; private readonly string rootName; private readonly Dictionary obj2id; private readonly Dictionary primitiveSerializers; private readonly List compoundSerializers; private readonly Dictionary typeCache; public Serializer(object obj) : this(obj, InterfaceInstantiatior.InstantiateAll(), InterfaceInstantiatior.InstantiateAll()) {} public Serializer(object obj, IEnumerable primitiveSerializers, IEnumerable compoundSerializers) : this(obj, primitiveSerializers, compoundSerializers, "ROOT") { } public Serializer(object obj, IEnumerable primitiveSerializers, IEnumerable compoundSerializers, string rootName) { this.obj = obj; this.rootName = rootName; this.primitiveSerializers = new Dictionary(); foreach (IPrimitiveSerializer serializer in primitiveSerializers) { this.primitiveSerializers.Add(serializer.Type, serializer); } this.compoundSerializers = new List(compoundSerializers); obj2id = new Dictionary {{new object(), 0}}; typeCache = new Dictionary(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public IEnumerator GetEnumerator() { DataMemberAccessor rootAccessor = new DataMemberAccessor( rootName, obj.GetType(), null, () => obj, null); IEnumerator iterator = Serialize(rootAccessor); while (iterator.MoveNext()) yield return iterator.Current; Console.WriteLine("TypeCache:f"); foreach ( var pair in typeCache ) Console.WriteLine(pair.Key); } private IEnumerator Serialize(DataMemberAccessor accessor) { object value = accessor.Get(); if (value == null) { yield return new NullReferenceToken(accessor.Name); yield break; } if (obj2id.ContainsKey(value)) { yield return new ReferenceToken(accessor.Name, obj2id[value]); yield break; } if ( ! typeCache.ContainsKey(value.GetType())) typeCache.Add(value.GetType(), typeCache.Count); int? id = null; if ( ! value.GetType().IsValueType) { id = obj2id.Count; obj2id.Add(value, (int)id); } if (primitiveSerializers.ContainsKey(value.GetType())) { yield return new PrimitiveToken( accessor, primitiveSerializers[value.GetType()].Serialize(value), id); yield break; } yield return new BeginToken(accessor, id); ICompoundSerializer customSerializer = FindCompoundSerializer(value.GetType()); if (customSerializer != null) { foreach (object o in customSerializer.Serialize(value)) { IEnumerator iterator = Serialize(new DataMemberAccessor(o)); while (iterator.MoveNext()) yield return iterator.Current; } yield return new EndToken(accessor, id); yield break; } int nSubComponents = 0; foreach (KeyValuePair mapping in StorableAttribute.GetAutostorableAccessors(value)) { nSubComponents += 1; IEnumerator iterator = Serialize(mapping.Value); while (iterator.MoveNext()) { yield return iterator.Current; } } if (nSubComponents == 0) { throw new ApplicationException(String.Format( "Composite value of type \"{0}\" contains no subcomponents", value.GetType().FullName)); } yield return new EndToken(accessor, id); } private ICompoundSerializer FindCompoundSerializer(Type type) { foreach (ICompoundSerializer s in compoundSerializers) { if (s.CanSerialize(type)) return s; } return null; } } }