zoukankan      html  css  js  c++  java
  • 把新建的对象所有属性变成默认值

    日常编码的过程中,我们经常能碰到一些小操作,有时候就是想节(tōu)约(gè)时(lǎn)间(er),所以写好工具方法方便直接调用

     1     public static class WipeNullHelper
     2     {
     3         /// <summary>
     4         /// 把对象里的null变成相应类型的默认值
     5         /// </summary>
     6         /// <param name="entity">对象实体</param>
     7         /// <returns></returns>
     8         public static T WipeNull<T>(T entity)
     9         {
    10             System.Reflection.PropertyInfo[] ps = entity.GetType().GetProperties();
    11             Type type = entity.GetType();
    12             foreach (PropertyInfo propertie in ps)
    13             {
    14                 if (propertie.PropertyType == typeof(string))
    15                 {
    16                     PropertyInfo propertyInfo = type.GetProperty(propertie.Name);
    17                     if (propertyInfo.GetValue(entity, null) == null)
    18                     {
    19                         propertyInfo.SetValue(entity, "", null);
    20                     }
    21                 }
    22                 else if (propertie.PropertyType == typeof(Int32))
    23                 {
    24                     PropertyInfo propertyInfo = type.GetProperty(propertie.Name);
    25                     if (propertyInfo.GetValue(entity, null) == null)
    26                     {
    27                         propertyInfo.SetValue(entity, 0, null);
    28                     }
    29                 }
    30                 else if (propertie.PropertyType == typeof(Nullable<int>))
    31                 {
    32                     PropertyInfo propertyInfo = type.GetProperty(propertie.Name);
    33                     if (propertyInfo.GetValue(entity, null) == null)
    34                     {
    35                         propertyInfo.SetValue(entity, 0, null);
    36                     }
    37                 }
    38                 else if (propertie.PropertyType == typeof(decimal))
    39                 {
    40                     PropertyInfo propertyInfo = type.GetProperty(propertie.Name);
    41                     if (propertyInfo.GetValue(entity, null) == null)
    42                     {
    43                         propertyInfo.SetValue(entity, Convert.ToDecimal(0.0), null);
    44                     }
    45                 }
    46                 else if (propertie.PropertyType == typeof(Nullable<decimal>))
    47                 {
    48                     PropertyInfo propertyInfo = type.GetProperty(propertie.Name);
    49                     if (propertyInfo.GetValue(entity, null) == null)
    50                     {
    51                         propertyInfo.SetValue(entity, Convert.ToDecimal(0.0), null);
    52                     }
    53                 }
    54             }
    55             return entity;
    56         }
    57 
    58     }
  • 相关阅读:
    网络编程练习 -- 文件上传
    网络编程练习 -- 大文件下载
    网络编程练习 -- NSURLConnection -- get/post请求
    IOS学习笔记 -- 网络编程
    WEB测试实践 第三天
    WEB测试实践 第二天
    WEB测试实践 第一天
    白盒测试实践(小组作业)第六天
    白盒测试实践(小组作业)第五天
    白盒测试实践(小组作业)第四天
  • 原文地址:https://www.cnblogs.com/JessieR/p/8953888.html
Copyright © 2011-2022 走看看