Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
 public PrimitiveNullableBuilder(Type elementType)
 {
     _elementType    = elementType;
     _elementBuilder = BuilderFactory.GetBuilder(_elementType, null, null);
 }
Exemplo n.º 4
0
 public EnumBuilder(Type enumType, Type enumBaseType)
 {
     _enumType     = enumType;
     _enumBaseType = enumBaseType;
     _baseBuilder  = BuilderFactory.GetBuilder(_enumBaseType, null, null);
 }