예제 #1
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var items = pipeline.Settings.EnumerableCount;

            var key       = context.RequestType.GetGenericArguments()[0];
            var valueType = context.RequestType.GetGenericArguments()[1];

            if (key.GetTypeInfo().IsEnum)
            {
                items = Enum.GetNames(key).Length;
            }

            var keys   = uniqueKeys();
            var values = itemValues(valueType);

            var dictionary = (IDictionary)typeof(Dictionary <,>).NewGeneric(key, valueType);

            for (var i = 0; i <= items - 1; i++)
            {
                dictionary.Add(keys[i], values[i]);
            }

            return(dictionary);

            List <object> uniqueKeys()
            {
                var builtKeys = new HashSet <object>();

                while (true)
                {
                    var newItem = pipeline.Send(new ConstruktionContext(key));

                    if (newItem != null)
                    {
                        builtKeys.Add(newItem);
                    }

                    if (builtKeys.Count == items)
                    {
                        return(builtKeys.ToList());
                    }
                }
            }

            List <object> itemValues(Type closedType)
            {
                var results = new List <object>();

                for (var i = 0; i < items; i++)
                {
                    results.Add(pipeline.Send(new ConstruktionContext(closedType)));
                }

                return(results);
            }
        }
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var implementation = _typeMap[context.RequestType];

            var result = pipeline.Send(new ConstruktionContext(implementation));

            return(result);
        }
예제 #3
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var closedType = context.RequestType.GetGenericArguments()[0];

            var useNull = _random.Next(1, 5);

            return(useNull == 1
                       ? null
                       : pipeline.Send(new ConstruktionContext(closedType)));
        }
예제 #4
0
        private T DoConstruct <T>(Type type, Action <T> hardCodes, ParameterInfo parameterInfo = null)
        {
            var context = type != null
                              ? new ConstruktionContext(type)
                              : new ConstruktionContext(parameterInfo);

            var result = (T)pipeline.Send(context);

            hardCodes?.Invoke(result);

            return(result);
        }
예제 #5
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var instance = newUp();

            var properties = pipeline.Settings.PropertyStrategy(context.RequestType);

            foreach (var property in properties)
            {
                var result = pipeline.Send(new ConstruktionContext(property));

                property.SetPropertyValue(instance, result);
            }

            return(instance);

            object newUp()
            {
                var ctors = context.RequestType.GetTypeInfo()
                            .DeclaredConstructors
                            .ToList();

                var ctor = pipeline.Settings.CtorStrategy(ctors);

                var @params = new List <object>();

                foreach (var parameter in ctor.GetParameters())
                {
                    var ctorArg = parameter.ParameterType;

                    var value = pipeline.Send(new ConstruktionContext(ctorArg));

                    @params.Add(value);
                }

                return(context.RequestType.NewUp(@params.ToArray()));
            }
        }
예제 #6
0
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var arrayType = context.RequestType.GetElementType();

            var array = Array.CreateInstance(arrayType, pipeline.Settings.EnumerableCount);

            for (var i = 0; i <= pipeline.Settings.EnumerableCount - 1; i++)
            {
                var value = pipeline.Send(new ConstruktionContext(arrayType));

                array.SetValue(value, i);
            }

            return(array);
        }
        public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline)
        {
            var closedType = context.RequestType.GenericTypeArguments[0];

            var items = (IList)typeof(List <>).NewGeneric(closedType);

            for (var i = 0; i < pipeline.Settings.EnumerableCount; i++)
            {
                var result = pipeline.Send(new ConstruktionContext(closedType));

                items.Add(result);
            }

            return(items);
        }