反射

     public static T NameValuesEntity<T>(NameValueCollection NVS)
        {
            T entity = Activator.CreateInstance<T>();
            PropertyInfo[] attrs = entity.GetType().GetProperties();
            //如果键值对为空则给他设置默认值bool类型的为FALSE
            if (!NVS.HasKeys())
            {
                foreach (PropertyInfo p in attrs)
                {
                    if (p.PropertyType.Name == "Boolean")
                    {
                        p.SetValue(entity, Convert.ChangeType(false, p.PropertyType), null);
                    }      
                }
            }
            else
            {
                foreach (PropertyInfo p in attrs)
                {
                    foreach (string key in NVS.AllKeys)
                    {
                        if (string.Compare(p.Name, key, true) == 0)
                        {
                            if (p.PropertyType.Name == "Boolean")//防止复选框的值是on
                            {
                                if (NVS[key] == "on")
                                {
                                    p.SetValue(entity, Convert.ChangeType(true, p.PropertyType), null);
                                }
                                else
                                {
                                    p.SetValue(entity, Convert.ChangeType(false, p.PropertyType), null);
                                }
                            }
                            else
                            {
                                p.SetValue(entity, Convert.ChangeType(NVS[key], p.PropertyType), null);
                            }

                        }
                    }
                }
            }  
            return entity;
        }

        public static ModuleConfig GetEntity(byte Type, NameValueCollection NVS)
        {
            foreach (Type type in Assembly.GetAssembly(typeof(ModuleConfig)).GetTypes().Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(ModuleConfig))))
            {
                var entity = (ModuleConfig)Activator.CreateInstance(type);
                if (entity.Type == Type)
                {
                    PropertyInfo[] attrs = entity.GetType().GetProperties();
                    if (!NVS.HasKeys())
                    {
                        foreach (PropertyInfo p in attrs)
                        {
                            if (p.PropertyType.Name == "Boolean")
                            {
                                p.SetValue(entity, Convert.ChangeType(false, p.PropertyType), null);
                            }
                        }
                    }
                    else
                    {
                        foreach (PropertyInfo p in attrs)
                        {
                            foreach (string key in NVS.AllKeys)
                            {
                                if (string.Compare(p.Name, key, true) == 0)
                                {
                                    if (p.PropertyType.Name == "Boolean")//防止复选框的值是on
                                    {
                                        if (NVS[key] == "on")
                                        {
                                            p.SetValue(entity, Convert.ChangeType(true, p.PropertyType), null);
                                        }
                                        else
                                        {
                                            p.SetValue(entity, Convert.ChangeType(false, p.PropertyType), null);
                                        }
                                    }
                                    else
                                    {
                                        p.SetValue(entity, Convert.ChangeType(NVS[key], p.PropertyType), null);
                                    }

                                }
                            }
                        }
                    }
                    return entity;
                   // return entity;
                }
            }
            return null;
        }

 

posted @ 2017-03-20 12:43  imfrank  阅读(149)  评论(0)    收藏  举报