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; } }
public EnumerableBuilder(Type enumerableType, Type baseElementType, TypeDescription elementTypeDescription, SerializerState state) { _baseElementType = baseElementType; _builderSpecificType = GetType(); _isStack = false; if (enumerableType.GetTypeInfo().IsGenericType&& enumerableType.GetGenericTypeDefinition() == typeof(IEnumerable <>)) { _enumerableType = baseElementType.MakeArrayType(); _isIEnumerable = true; } else { _enumerableType = enumerableType; _isIEnumerable = false; if (enumerableType.GetTypeInfo().IsGenericType&& enumerableType.GetGenericTypeDefinition() == typeof(Stack <>)) { _isStack = true; } } var ienumerableSpecificType = typeof(IEnumerable <>).MakeGenericType(baseElementType); var argExp = Expression.Parameter(ienumerableSpecificType); Func <Type, Expression> getConstructorExp = type => Expression.New(type.GetTypeInfo().GetConstructor(new[] { ienumerableSpecificType }), argExp); var enumerableTypes = new Dictionary <Type, Func <Type, Expression> > { [baseElementType.MakeArrayType()] = type => argExp, [typeof(List <>).MakeGenericType(baseElementType)] = getConstructorExp, [typeof(HashSet <>).MakeGenericType(baseElementType)] = getConstructorExp, [typeof(LinkedList <>).MakeGenericType(baseElementType)] = getConstructorExp, [typeof(Queue <>).MakeGenericType(baseElementType)] = getConstructorExp, [typeof(SortedSet <>).MakeGenericType(baseElementType)] = getConstructorExp, [typeof(Stack <>).MakeGenericType(baseElementType)] = getConstructorExp }; _constructorsByIndex = new Func <IEnumerable <T>, IEnumerable <T> > [enumerableTypes.Count()]; _constructorsByType = new AdaptiveHashtable <Constructor>(); var index = (ushort)0; foreach (var item in enumerableTypes) { var specificType = item.Key; var constructorExp = item.Value(specificType); var method = Expression.Lambda <Func <IEnumerable <T>, IEnumerable <T> > >(constructorExp, argExp).Compile(); _constructorsByIndex[index] = method; _constructorsByType.AddValue( (uint)RuntimeHelpers.GetHashCode(specificType), new Constructor { Index = index++, Method = method }); } _elementBuilder = BuilderFactory.GetBuilder(baseElementType, elementTypeDescription, state); }
public DictionaryBuilder(Type dictionaryType, Type keyType, Type valType, TypeDescription keyTypeDescription, TypeDescription valTypeDescription, SerializerState state) { _keyType = keyType; _valType = valType; _builderSpecificType = GetType(); if (dictionaryType.GetTypeInfo().IsGenericType&& dictionaryType.GetGenericTypeDefinition() == typeof(IDictionary <,>)) { _dictionaryType = typeof(Dictionary <,>).MakeGenericType(keyType, valType); } else { _dictionaryType = dictionaryType; } var dictionarySpecificType = typeof(IDictionary <,>).MakeGenericType(keyType, valType); var argExp = Expression.Parameter(dictionarySpecificType); Func <Type, Expression> getConstructorExp = type => Expression.New(type.GetTypeInfo().GetConstructor(new[] { dictionarySpecificType }), argExp); var enumerableTypes = new Dictionary <Type, Func <Type, Expression> > { [typeof(Dictionary <,>).MakeGenericType(keyType, valType)] = getConstructorExp, [typeof(SortedList <,>).MakeGenericType(keyType, valType)] = getConstructorExp, [typeof(SortedDictionary <,>).MakeGenericType(keyType, valType)] = getConstructorExp }; _constructorsByIndex = new Func <IDictionary <TKey, TVal>, IDictionary <TKey, TVal> > [enumerableTypes.Count()]; _constructorsByType = new AdaptiveHashtable <Constructor>(); var index = (ushort)0; foreach (var item in enumerableTypes) { var specificType = item.Key; var constructorExp = item.Value(specificType); var method = Expression.Lambda <Func <IDictionary <TKey, TVal>, IDictionary <TKey, TVal> > >(constructorExp, argExp).Compile(); _constructorsByIndex[index] = method; _constructorsByType.AddValue( (uint)RuntimeHelpers.GetHashCode(specificType), new Constructor { Index = index++, Method = method }); } _keyBuilder = BuilderFactory.GetBuilder(keyType, keyTypeDescription, state); _valBuilder = BuilderFactory.GetBuilder(valType, valTypeDescription, state); }
internal SerializerState() { AllTypeDescriptions = new Dictionary <Type, TypeDescription>(); _descriptions = new AdaptiveHashtable <TypeDescription>(); }