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     }
  • 相关阅读:
    学习方式的反省
    我又辞职了!
    mysql完全备份,增量备份及恢复脚本
    marquee.js jQuery 多功能无缝滚动插件
    ul与li应用样式及其兼容性
    闲谈 JavaScript 之事件绑定(转载自万戈)
    JavaScript 中万变不离其宗的 this 应用场景
    ScrollUp – 超轻量的可定制的回到顶部 jQuery 插件
    jQuery之Tab切换代码改进
    jQuery Unveil – 另一款非常轻量的延迟加载插件
  • 原文地址:https://www.cnblogs.com/JessieR/p/8953888.html
Copyright © 2011-2022 走看看