コード例 #1
0
        /// <summary>
        /// Convert NameValueCollection to a Entity object
        /// </summary>
        /// <param name="nv"></param>
        /// <param name="obj_type"></param>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public static object NameValueCollection2Entity(NameValueCollection nv, Type obj_type, String prefix)
        {
            if (nv == null || nv.Count == 0)
            {
                return(null);
            }

            // create a intance for this object and get all property
            object instance = Activator.CreateInstance(obj_type);

            PropertyInfo[] pi = instance.GetType().GetProperties();

            // in first invoke, prefix is null
            String name = (prefix == null) ? "" : (prefix + ".");

            foreach (PropertyInfo p in pi)
            {
                try
                {
                    if (StringConverter.CanConvertDirectly(p.PropertyType))
                    {
                        //must check exist this key , otherwise exception will be thrown
                        if (nv.AllKeys.Contains(name + p.Name))
                        {
                            p.SetValue(instance,
                                       StringConverterManager.ConvertTo(nv[name + p.Name], p.PropertyType),
                                       null);
                        }
                    }
                    else if (p.PropertyType.IsArray)
                    {
                        //arrary
                    }
                    else if (p.PropertyType.IsClass)
                    {
                        //class
                        p.SetValue(instance,
                                   NameValueCollection2Entity(nv, p.PropertyType, name + p.Name),
                                   null);
                    }
                }
                catch (Exception) { }
            }
            return(instance);
        }