zoukankan      html  css  js  c++  java
  • 将表单赋予对对象

     
         /// <summary>
            ///  将表单赋予对对象 
            /// </summary>
            /// <typeparam name="T">类型参数</typeparam>
            /// <param name="t">实体对象</param>
            /// <param name="form">表单集合</param>
            public static void GetValue<T>(T t, NameValueCollection form)
            {
                Type type = t.GetType();
                PropertyInfo[] pi = type.GetProperties();
                foreach (PropertyInfo p in pi)
                {
                    if (!string.IsNullOrEmpty(form[p.Name]))
                    {
                        //可空类型
                        if (p.PropertyType.FullName == "System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")
                        {
    
                            if (!string.IsNullOrEmpty(form[p.Name]))
                            {
                                p.SetValue(t, Convert.ToDateTime(form[p.Name]), null);
                            }
                        }
                        else if (p.PropertyType.FullName == "System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")
                        {
                            if (!string.IsNullOrEmpty(form[p.Name]))
                            {
                                p.SetValue(t, Convert.ToDateTime(form[p.Name]), null);
                            }
                        }
                        else if (p.PropertyType.FullName == "System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")
                        {
                            if (!string.IsNullOrEmpty(form[p.Name]))
                            {
                                p.SetValue(t, Convert.ToInt32(form[p.Name]), null);
                            }
                        }
                        else if (p.PropertyType.FullName == "System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")
                        {
                            if (!string.IsNullOrEmpty(form[p.Name]))
                            {
                                p.SetValue(t, Convert.ToInt32(form[p.Name]), null);
                            }
                        }
                        else if (p.PropertyType.FullName == "System.Nullable`1[[System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")
                        {
                            if (!string.IsNullOrEmpty(form[p.Name]))
                            {
                                p.SetValue(t, Convert.ToInt64(form[p.Name]), null);
                            }
                        }
                        else if (p.PropertyType.FullName == "System.Nullable`1[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")
                        {
                            if (!string.IsNullOrEmpty(form[p.Name]))
                            {
                                p.SetValue(t, Convert.ToInt64(form[p.Name]), null);
                            }
                        }
                        else if (p.PropertyType.FullName == "System.Nullable`1[[System.Decimal, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")
                        {
                            if (!string.IsNullOrEmpty(form[p.Name]))
                            {
                                p.SetValue(t, Convert.ToDecimal(form[p.Name]), null);
                            }
                        }
                        else if (p.PropertyType.FullName == "System.Nullable`1[[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")
                        {
                            if (!string.IsNullOrEmpty(form[p.Name]))
                            {
                                p.SetValue(t, Convert.ToDecimal(form[p.Name]), null);
                            }
                        }
                        else if (p.PropertyType.FullName == "System.Nullable`1[[System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")
                        {
                            if (!string.IsNullOrEmpty(form[p.Name]))
                            {
                                p.SetValue(t, Convert.ToBoolean(form[p.Name]), null);
                            }
                        }
                        else if (p.PropertyType.FullName == "System.Nullable`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")
                        {
                            if (!string.IsNullOrEmpty(form[p.Name]))
                            {
                                p.SetValue(t, Convert.ToBoolean(form[p.Name]), null);
                            }
                        }
                        else
                        {
                            p.SetValue(t, Convert.ChangeType(form[p.Name], p.PropertyType), null);
                        }
    
                    }
                }
            }
  • 相关阅读:
    centos安装Libzip
    PHP安装
    DWZ主从表界面唯一性验证(自写js)(一)
    JSTL的if...else项目小试
    分享:忙了一辈子,才知道什么是你的
    JSTL时间格式化项目小试
    eclipse中去掉validate的方法
    Java初学者必学的JSTL
    (转)Java程序员应该知道的10个调试技巧
    为什么那些美事没有实现---生活中小事有感
  • 原文地址:https://www.cnblogs.com/haomo/p/6243325.html
Copyright © 2011-2022 走看看