Beispiel #1
0
        public CollectionMembers(ExtendedType collectionType)
        {
            ArrayContainerTypeInfo arrayTypeInfo;

            if (collectionType.TryGetArrayTypeInfo(out arrayTypeInfo))
            {
                if (arrayTypeInfo.Ranks > 3)
                {
                    throw new NotSupportedException("The serialization engine is limited to 3 ranks in arrays");
                }
                if (arrayTypeInfo.Ranks == 3)
                {
                    var baseType = typeof(ICollection <>);
                    ElementType = baseType.MakeGenericType(baseType.MakeGenericType(arrayTypeInfo.ElementType));
                    ToArray     = typeof(ArrayProvider).GetTypeInfo().GetMethod("To3DArray").MakeGenericMethod(arrayTypeInfo.ElementType);
                }
                else if (arrayTypeInfo.Ranks == 2)
                {
                    ElementType = typeof(ICollection <>).MakeGenericType(arrayTypeInfo.ElementType);
                    ToArray     = typeof(ArrayProvider).GetTypeInfo().GetMethod("To2DArray").MakeGenericMethod(arrayTypeInfo.ElementType);
                }
                else
                {
                    ElementType = arrayTypeInfo.ElementType;
                    ToArray     = typeof(ArrayProvider).GetTypeInfo().GetMethod("ToArray").MakeGenericMethod(arrayTypeInfo.ElementType);
                }
            }
            else
            {
                ElementType = collectionType.Container.AsCollection().ElementType;
            }

            ElementTypeExt = collectionType.Provider.Extend(ElementType);
            VariableType   = typeof(ICollection <>).MakeGenericType(ElementType);

            Add = VariableType.GetTypeInfo().GetMethod("Add", new[] { ElementType });
            var instanceType = collectionType.Info.IsInterface || collectionType.Ref.IsArray
                ? typeof(List <>).MakeGenericType(ElementType)
                : collectionType.Ref;

            Constructor = instanceType.GetTypeInfo().GetConstructor(Type.EmptyTypes);
            if (Constructor == null)
            {
                throw InvalidGraphException.NoParameterLessConstructor(collectionType.Ref);
            }
        }
        private void GenerateCreateAndChildCallCode(ILLocalVariable local)
        {
            var type = local.Type;

            var constructor = type.GetTypeInfo().GetConstructor(Type.EmptyTypes);

            if (constructor == null)
            {
                throw InvalidGraphException.NoParameterLessConstructor(type);
            }

            _il.Construct(constructor);
            _il.Set(local);

            var childTravellerInfo = _context.GetTraveller(type);

            var field = ILPointer.Field(ILPointer.This(), childTravellerInfo.Field);

            _il.InvokeMethod(field, childTravellerInfo.TravelReadMethod, _visitorVariable, local);
        }
Beispiel #3
0
        public DictionaryMembers(ExtendedType dictionaryType)
        {
            var container = dictionaryType.Container.AsDictionary();

            KeyType      = container.KeyType;
            ValueType    = container.ValueType;
            ElementType  = container.ElementType;
            VariableType = typeof(IDictionary <,>).MakeGenericType(KeyType, ValueType);

            Add = VariableType.GetTypeInfo().GetMethod("Add", new[] { KeyType, ValueType });
            var instanceType = dictionaryType.Info.IsInterface
                ? typeof(Dictionary <,>).MakeGenericType(KeyType, ValueType)
                : dictionaryType.Ref;

            Constructor = instanceType.GetTypeInfo().GetConstructor(Type.EmptyTypes);
            if (Constructor == null)
            {
                throw InvalidGraphException.NoParameterLessConstructor(dictionaryType.Ref);
            }
        }