예제 #1
0
        protected override void When()
        {
            var binder        = BinderCollection.FindBinderFor("json", typeof(Json), this.Request, new RouteValues(), null);
            var bindingResult = binder.Bind("json", typeof(Json), this.Request, new RouteValues(), false, null);

            this.target = (Json)bindingResult.Object;
        }
        protected override void When()
        {
            var binder        = BinderCollection.FindBinderFor("datey", typeof(DateTime), this.Request, new RouteValues(), null);
            var bindingResult = binder.Bind("datey", typeof(bool?), this.Request, new RouteValues(), false, null);

            this.target = (DateTime)bindingResult.Object;
        }
예제 #3
0
        protected override void When()
        {
            var binder        = BinderCollection.FindBinderFor(null, typeof(IEnumerable <IFile>), this.Request, new RouteValues(), null);
            var bindingResult = binder.Bind("files", typeof(IEnumerable <IFile>), this.Request, new RouteValues(), false, null);

            this.target = bindingResult.Object as IEnumerable <IFile>;
        }
        protected override void When()
        {
            var binder        = BinderCollection.FindBinderFor(null, typeof(InnerClass), this.Request, new RouteValues(), null);
            var bindingResult = binder.Bind("parameter", typeof(InnerClass), this.Request, new RouteValues(), false, null);

            this.target = bindingResult.Object as InnerClass;
        }
        public override bool CanBind(string name, Type outerType, IRequest request, RouteValues routeValues, object owner)
        {
            if (!outerType.IsGenericType)
            {
                return(false);
            }

            var genericOuterType = outerType.GetGenericTypeDefinition();

            if (genericOuterType != typeof(IEnumerable <>))
            {
                return(false);
            }

            var middleType = outerType.GetGenericArguments()[0];

            if (!middleType.IsGenericType)
            {
                return(false);
            }

            var genericMiddleType = middleType.GetGenericTypeDefinition();

            if (genericMiddleType != typeof(IEnumerable <>))
            {
                return(false);
            }

            var innerType = middleType.GetGenericArguments()[0];

            var innerBinder = BinderCollection.FindBinderFor(null, innerType, request, routeValues, owner);

            return(innerBinder != null);
        }
예제 #6
0
        public BindingResult Bind(Type type, string name, string value, IRequest request, RouteValues routeValues, object owner)
        {
            var bindingErrors = new List <BindingError>();

            var innerType = GetEnumerableInnerType(type);

            var list = new List <object>();

            var innerBinder = BinderCollection.FindBinderFor(null, innerType, request, routeValues, owner);

            var stringValue = value;

            if (!string.IsNullOrEmpty(stringValue))
            {
                var strings = stringValue.Split(innerType == typeof(string) ? '\n' : ',');
                foreach (var str in strings)
                {
                    var bindingResult = innerBinder.Bind(innerType, name, str, request, routeValues, owner);

                    if (bindingResult.Result != BindingResult.ResultType.Failure)
                    {
                        list.Add(bindingResult.Object);
                    }
                    else
                    {
                        bindingErrors.AddRange(bindingResult.BindingErrors);
                    }
                }
            }

            var enumerable = list.Cast(innerType);

            return(new BindingResult(enumerable, bindingErrors.ToArray(), bindingErrors.Any() ? BindingResult.ResultType.Failure : BindingResult.ResultType.Success));
        }
        protected override void When()
        {
            var binder        = BinderCollection.FindBinderFor(null, typeof(NullableEnumTestViewModel), this.Request, new RouteValues(), null);
            var bindingResult = binder.Bind("parameter", typeof(NullableEnumTestViewModel), this.Request, new RouteValues(), false, null);

            this.target = (NullableEnumTestViewModel)bindingResult.Object;
        }
예제 #8
0
        static IEnumerable <BindingError> SetProperties(string prefix, Type type, object instance, IRequest request, RouteValues routeValues, object owner)
        {
            var bindingErrors = new List <BindingError>();

            var settableProperties = GetSettableProperties(type);

            foreach (var property in settableProperties)
            {
                var typeSpecifierName = (prefix != null ? prefix + "." + property.Name : property.Name) + ".$Type";
                var propertyType      = GetTypeFromTypeSpecifier(typeSpecifierName, request, routeValues, owner) ?? property.PropertyType;

                var binder = BinderCollection.FindBinderFor(property.Name, propertyType, request, routeValues, owner);

                if (binder == null)
                {
                    continue;
                }

                var name = prefix != null ? prefix + "." + property.Name : property.Name;

                var bindingResult = binder.Bind(name, property.PropertyType, request, routeValues, true, owner);

                property.SetValue(instance, bindingResult.Object, null);

                if (bindingResult.Result == BindingResult.ResultType.Failure)
                {
                    bindingErrors.AddRange(bindingResult.BindingErrors);
                }
            }

            return(bindingErrors);
        }
예제 #9
0
        public BindingResult Bind(string name, Type type, IRequest request, RouteValues routeValues, bool isNestedCall, object owner)
        {
            var prefix        = name + "[";
            var bindingErrors = new List <BindingError>();

            var genericArgument = type.GetGenericArguments()[0];

            var list = Activator.CreateInstance(typeof(ExpandableList <>).MakeGenericType(genericArgument)) as IList;

            foreach (var key in request.Form.AllKeys)
            {
                if (key.StartsWith(prefix, true, CultureInfo.InvariantCulture))
                {
                    var start = prefix.Length;
                    var end   = key.IndexOf(']', start);

                    var index = int.Parse(key.Substring(start, end - start));

                    var indexedName = name + "[" + index + "]";
                    var innerBinder = BinderCollection.FindBinderFor(indexedName, genericArgument, request, routeValues, owner);
                    var bind        = innerBinder.Bind(indexedName, genericArgument, request, routeValues, true, owner);

                    list[index] = bind.Object;

                    if (bind.Result == BindingResult.ResultType.Failure)
                    {
                        bindingErrors.AddRange(bind.BindingErrors);
                    }
                }
            }

            return(new BindingResult(list, bindingErrors.ToArray(), bindingErrors.Any() ? BindingResult.ResultType.Failure : BindingResult.ResultType.Success));
        }
예제 #10
0
        public override BindingResult Bind(Type type, string name, string value, IRequest request, RouteValues routeValues, object owner)
        {
            var underlyingType = Nullable.GetUnderlyingType(type);

            if (string.IsNullOrEmpty(value))
            {
                return(new BindingResult(null, BindingResult.ResultType.Default));
            }

            var binder = BinderCollection.FindBinderFor(name, underlyingType, request, routeValues, owner);

            return(binder.Bind(type, name, value, request, routeValues, owner));
        }
        public override BindingResult Bind(Type type, string name, string value, IRequest request, RouteValues routeValues, object owner)
        {
            var middleType  = type.GetGenericArguments()[0];
            var innerType   = middleType.GetGenericArguments()[0];
            var innerBinder = BinderCollection.FindBinderFor(null, innerType, request, routeValues, owner);

            var bound    = new List <List <object> >();
            var strValue = value;

            if (!string.IsNullOrEmpty(strValue))
            {
                var rows = strValue.Split('\n');

                foreach (var row in rows)
                {
                    var cleanRow = row.Trim();

                    if (cleanRow == string.Empty)
                    {
                        continue;
                    }

                    var newRow = new List <object>();

                    var cells = cleanRow.Split(',');

                    foreach (var cell in cells)
                    {
                        var bindingResult = innerBinder.Bind(innerType, name, cell, request, routeValues, owner);

                        if (bindingResult.Result != BindingResult.ResultType.Failure)
                        {
                            newRow.Add(bindingResult.Object);
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }
                    }
                    bound.Add(newRow);
                }
            }

            var enumerable = bound.Select(inner => inner.Cast(innerType));
            var result     = enumerable.Cast(typeof(IEnumerable <>).MakeGenericType(innerType));

            return(new BindingResult(result));
        }
예제 #12
0
        static Type GetTypeFromTypeSpecifier(string name, IRequest request, RouteValues routeValues, object owner)
        {
            var binder = BinderCollection.FindBinderFor(name, typeof(string), request, routeValues, owner);

            if (binder == null)
            {
                return(null);
            }

            var bindingResult = binder.Bind(name, typeof(string), request, routeValues, true, owner);

            if (bindingResult.Result == BindingResult.ResultType.Success && bindingResult.Object as string != null)
            {
                return(Type.GetType((string)bindingResult.Object, false) ?? null);
            }

            return(null);
        }
예제 #13
0
        public BindingResult Bind(Type type, string name, string value, IRequest request, RouteValues routeValues, object owner)
        {
            var bindingErrors = new List <BindingError>();

            var keyType   = GetGenericType(type, 0);
            var valueType = GetGenericType(type, 1);

            var keyBinder   = BinderCollection.FindBinderFor(null, keyType, request, routeValues, owner);
            var valueBinder = BinderCollection.FindBinderFor(null, valueType, request, routeValues, owner);

            Regex dictKeyRegex = new Regex("^" + Regex.Escape(name) + "\\[([^]]+)\\]");

            var matchedKeys = request.Form.AllKeys.Select(x => dictKeyRegex.Match(x)).Where(r => r.Success).Distinct(x => x.Value).ToArray();

            var dictType = typeof(Dictionary <,>).MakeGenericType(keyType, valueType);
            var dict     = Activator.CreateInstance(dictType) as IDictionary;

            foreach (var matchedKey in matchedKeys)
            {
                var dictKeyPart = matchedKey.Groups[1].Value;
                var formKey     = matchedKey.Value;

                var keyBinding   = keyBinder.Bind(keyType, formKey, dictKeyPart, request, routeValues, owner);
                var valueBinding = valueBinder.Bind(formKey, valueType, request, routeValues, true, owner);

                if (keyBinding.Result == BindingResult.ResultType.Success && valueBinding.Result == BindingResult.ResultType.Success)
                {
                    dict.Add(keyBinding.Object, valueBinding.Object);
                }
                else
                {
                    bindingErrors.AddRange(keyBinding.BindingErrors);
                    bindingErrors.AddRange(valueBinding.BindingErrors);
                }
            }

            return(new BindingResult(dict, bindingErrors.ToArray(), bindingErrors.Any() ? BindingResult.ResultType.Failure : BindingResult.ResultType.Success));
        }