예제 #1
0
        /// <summary>
        /// Bind the model
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>
        /// An object of the specified type (<seealso cref="IModelBinderContext.ModelType)" />
        /// </returns>
        public object Bind(IModelBinderContext context)
        {
            if (context.ModelType.GetConstructor(new Type[0]) == null)
            {
                throw new ModelBindingException("Do not have a default constructor.", context.ModelName, context.ModelType);
            }

            var model  = Activator.CreateInstance(context.ModelType);
            var prefix = string.IsNullOrEmpty(context.Prefix) ? context.ModelName : context.Prefix + "." + context.ModelName;

            foreach (var property in context.ModelType.GetProperties())
            {
                if (!property.CanWrite)
                {
                    continue;
                }

                var value = context.Execute(property.PropertyType, prefix, property.Name);


                property.SetValue(model, value, null);
            }

            return(model);
        }
예제 #2
0
        private object BindGeneric <TValueType>(IModelBinderContext context)
        {
            var fieldName = context.Prefix + context.ModelName + "[";
            var fields    = new List <string>();

            foreach (var item in context.ValueProvider.Find(fieldName))
            {
                var pos = item.Name.IndexOf(']', fieldName.Length);
                if (pos == -1)
                {
                    throw new FormatException("Expected to find ']' to mark end of array in " + item.Name);
                }

                var name = item.Name.Substring(0, pos + 1);
                if (!fields.Contains(name))
                {
                    fields.Add(name);
                }
            }
            if (!fields.Any())
            {
                return(null);
            }

            var model = new Dictionary <string, TValueType>();

            for (var i = 0; i < fields.Count; i++)
            {
                var value   = (TValueType)context.Execute(typeof(TValueType), context.Prefix, fields[i]);
                var pos     = fields[i].IndexOf('[');
                var indexer = fields[i].Substring(pos + 1).TrimEnd(']').Trim('\'');
                model.Add(indexer, value);
            }
            return(model);
        }
예제 #3
0
        private static object BuildIndexedArray(IModelBinderContext context, string fieldName, Type arrayType)
        {
            var fields  = new List <string>();
            var indexes = new List <int>();

            foreach (var item in context.ValueProvider.Find(fieldName))
            {
                var pos = item.Name.IndexOf(']', fieldName.Length);
                if (pos == -1)
                {
                    throw new ModelBindingException("Expected to find ']' to mark end of array.", fieldName + "]", item.Name);
                }

                var name = item.Name.Substring(0, pos + 1);
                if (!fields.Contains(name))
                {
                    fields.Add(name);

                    var index = ExtractIndex(name, fieldName);
                    indexes.Add(index);
                }
            }

            if (!fields.Any())
            {
                return(null);
            }

            ValidateIndexes(indexes, fieldName);

            var array = Array.CreateInstance(arrayType, fields.Count);

            for (var i = 0; i < fields.Count; i++)
            {
                var index = indexes[i];

                //prefix already includes the field
                var value = context.Execute(arrayType, "", fields[i]);
                array.SetValue(value, index);
            }

            return(array);
        }
예제 #4
0
        /// <summary>
        /// Bind the model
        /// </summary>
        /// <param name="context">Context information</param>
        /// <returns>
        /// An object of the specified type (<seealso cref="IModelBinderContext.ModelType)" />
        /// </returns>
        public object Bind(IModelBinderContext context)
        {
            if (context.ModelType.GetConstructor(new Type[0]) == null)
                throw new ModelBindingException("Do not have a default constructor.", context.ModelName, context.ModelType);

            var model = Activator.CreateInstance(context.ModelType);
            var prefix = string.IsNullOrEmpty(context.Prefix) ? context.ModelName : context.Prefix + "." + context.ModelName;
            foreach (var property in context.ModelType.GetProperties())
            {
                if (!property.CanWrite)
                    continue;

                var value = context.Execute(property.PropertyType, prefix, property.Name);

                
                property.SetValue(model, value, null);

            }

            return model;
        }
        private static object BuildIndexedArray(IModelBinderContext context, string fieldName, Type arrayType)
        {
            var fields = new List<string>();
            var indexes = new List<int>();

            foreach (var item in context.ValueProvider.Find(fieldName))
            {
                var pos = item.Name.IndexOf(']', fieldName.Length);
                if (pos == -1)
                    throw new ModelBindingException("Expected to find ']' to mark end of array.", fieldName + "]", item.Name);

                var name = item.Name.Substring(0, pos + 1);
                if (!fields.Contains(name))
                {
                    fields.Add(name);

                    var index = ExtractIndex(name, fieldName);
                    indexes.Add(index);
                }
            }

            if (!fields.Any())
                return null;

            ValidateIndexes(indexes, fieldName);

            var array = Array.CreateInstance(arrayType, fields.Count);
            for (var i = 0; i < fields.Count; i++)
            {
                var index = indexes[i];

                //prefix already includes the field
                var value = context.Execute(arrayType, "", fields[i]);
                array.SetValue(value, index);
            }

            return array;
        }