• 将表单赋予对对象


     
         /// <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);
                        }
    
                    }
                }
            }
  • 相关阅读:
    一次linux启动故障记录
    linux 时间相关的一些总结
    linux 3.10 gro的理解和改进
    linux 3.10 的中断收包笔记
    一个有趣的nginx问题引发的小问题
    linux 3.10 tcp的accept测试
    linux mce的一些相关内容和用户态监控的设计方法
    C/C++(基础-运算符详解)
    C/C++(基础-常量,类型转换)
    C/C++(基础编码-补码详解)
  • 原文地址:https://www.cnblogs.com/haomo/p/6243325.html
Copyright © 2020-2023  润新知