public PolymorphicComplexBuilder(Type type, SerializerState state)
        {
            _typeDescriptionsByHashCode = new AdaptiveHashtable <TypeDescriptionWithIndex>();
            var typeDescriptions = new Dictionary <int, TypeDescriptionWithIndex>();

            foreach (var description in state.GetDescriptionsForDerivedTypes(type))
            {
                _typeDescriptionsByHashCode.AddValue(
                    (uint)RuntimeHelpers.GetHashCode(description.Type),
                    new TypeDescriptionWithIndex
                {
                    Description = description
                });

                if (!typeDescriptions.ContainsKey(description.Type.GetHashCode()))
                {
                    typeDescriptions.Add(description.Type.GetHashCode(), new TypeDescriptionWithIndex
                    {
                        Description = description
                    });
                }
            }
            _typeDescriptionsByIndex = new TypeDescription[typeDescriptions.Count];
            var index = (byte)0;

            foreach (var item in typeDescriptions)
            {
                item.Value.Index = index;
                var val = _typeDescriptionsByHashCode.TryGetValue((uint)RuntimeHelpers.GetHashCode(item.Value.Description.Type));
                Debug.Assert(val != null, "Type should exist in the type descriptions hash");
                val.Index = index;
                _typeDescriptionsByIndex[index++] = item.Value.Description;
            }
        }
Esempio n. 2
0
        internal void SerializeEnumerable(IEnumerable <T> enumerable, BinaryWriter writer)
        {
            if (enumerable == null)
            {
                writer.Write(NullLength);
                return;
            }

            writer.Write(enumerable.Count());
            var constructorIndex = _constructorsByType.TryGetValue((uint)RuntimeHelpers.GetHashCode(enumerable.GetType())).Index;

            writer.Write(constructorIndex);
            foreach (var item in enumerable)
            {
                _elementSerializer(item, writer);
            }
        }
Esempio n. 3
0
        internal void Serialize(IDictionary <TKey, TVal> dictionary, Writer writer)
        {
            if (dictionary == null)
            {
                writer.Write(NullLength);
                return;
            }

            writer.Write(dictionary.Count());
            var constructorIndex = _constructorsByType.TryGetValue((uint)RuntimeHelpers.GetHashCode(dictionary.GetType())).Index;

            writer.Write(constructorIndex);
            foreach (var item in dictionary)
            {
                _keySerializer(item.Key, writer);
                _valSerializer(item.Value, writer);
            }
        }
Esempio n. 4
0
 internal TypeDescription GetDescription(Type type)
 {
     Debug.Assert(_descriptions != null, "Descriptions can't be null");
     return(_descriptions.TryGetValue((uint)type.TypeHandle.GetHashCode()));
 }