예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Ceen.Mvc.Controller.MethodEntry"/> struct.
        /// </summary>
        /// <param name="method">The method to wrap.</param>
        /// <param name="variables">The variables to include</param>
        public MethodEntry(MethodInfo method, IEnumerable <KeyValuePair <string, bool> > variables)
        {
            Method        = method;
            ArgumentCount = method.GetParameters().Length;

            var urlargs = (variables ?? new KeyValuePair <string, bool> [0]).ToLookup(x => x.Key, x => x.Value);

            var i = 0;

            Parameters = Method
                         .GetParameters()
                         .Select(par =>
            {
                var name_attr = par.GetCustomAttributes(typeof(NameAttribute), false).Cast <NameAttribute>().FirstOrDefault();
                var name      = name_attr == null || string.IsNullOrWhiteSpace(name_attr.Name) ? par.Name : name_attr.Name;

                var par_attr = par.GetCustomAttributes(typeof(ParameterAttribute), false).Cast <ParameterAttribute>().FirstOrDefault();
                ParameterSource source;
                bool optional;

                if (par_attr == null)
                {
                    if (urlargs.Contains(name))
                    {
                        source   = ParameterSource.Url;
                        optional = urlargs[name].First();
                    }
                    else
                    {
                        source   = ParameterSource.Default;
                        optional = par.HasDefaultValue;
                    }
                }
                else
                {
                    source   = par_attr.Source;
                    optional = !par_attr.Required;
                    if (!string.IsNullOrWhiteSpace(par_attr.Name))
                    {
                        name = par_attr.Name;
                    }
                }

                var pe = new ParameterEntry(par, name, source, !optional, i);

                i++;

                return(pe);
            })
                         .ToArray();;
        }
예제 #2
0
        /// <summary>
        /// Applies the argument to the value list.
        /// </summary>
        /// <param name="entry">The argument entry.</param>
        /// <param name="method">The method to apply to.</param>
        /// <param name="value">The argument value.</param>
        /// <param name="values">The list of values to process.</param>
        private static void ApplyArgument(MethodInfo method, ParameterEntry entry, string value, object[] values)
        {
            var argtype = method.GetParameters()[entry.ArgumentIndex].ParameterType;

            try
            {
                if (argtype.IsPrimitive || argtype.IsEnum || argtype == typeof(string))
                {
                    values[entry.ArgumentIndex] = Convert.ChangeType(value, argtype);
                }
                else
                {
                    Newtonsoft.Json.JsonConvert.DeserializeObject(value, argtype);
                }
            }
            catch (Exception)
            {
                throw new HttpException(HttpStatusCode.BadRequest, $"The value \"{value}\" for {entry.Name} is not a valid {argtype.Name.ToLower()}");
            }
        }