zoukankan      html  css  js  c++  java
  • [转]c#中如何利用反射设置属性值

    本文转自:http://blog.csdn.net/donetren/article/details/5270594

    ///<summary>
    2         /// 设置相应属性的值
    3         ///</summary>
    4         ///<param name="entity">实体</param>
    5         ///<param name="fieldName">属性名</param>
    6         ///<param name="fieldValue">属性值</param>
    7         publicstaticvoid SetValue(object entity, string fieldName, string fieldValue)
    8         {
    9             Type entityType = entity.GetType();
    10
    11             PropertyInfo propertyInfo = entityType.GetProperty(fieldName);
    12
    13             if (IsType(propertyInfo.PropertyType, "System.String"))
    14             {
    15                 propertyInfo.SetValue(entity, fieldValue, null);
    16
    17             }
    18
    19             if (IsType(propertyInfo.PropertyType, "System.Boolean"))
    20             {
    21                 propertyInfo.SetValue(entity, Boolean.Parse(fieldValue), null);
    22
    23             }
    24
    25             if (IsType(propertyInfo.PropertyType, "System.Int32"))
    26             {
    27                 if (fieldValue !="")
    28                     propertyInfo.SetValue(entity, int.Parse(fieldValue), null);
    29                 else
    30                     propertyInfo.SetValue(entity, 0, null);
    31
    32             }
    33
    34             if (IsType(propertyInfo.PropertyType, "System.Decimal"))
    35             {
    36                 if (fieldValue !="")
    37                     propertyInfo.SetValue(entity, Decimal.Parse(fieldValue), null);
    38                 else
    39                     propertyInfo.SetValue(entity, new Decimal(0), null);
    40
    41             }
    42
    43             if (IsType(propertyInfo.PropertyType, "System.Nullable`1[System.DateTime]"))
    44             {
    45                 if (fieldValue !="")
    46                 {
    47                     try
    48                     {
    49                         propertyInfo.SetValue(
    50                             entity,
    51                             (DateTime?)DateTime.ParseExact(fieldValue, "yyyy-MM-dd HH:mm:ss", null), null);
    52                     }
    53                     catch
    54                     {
    55                         propertyInfo.SetValue(entity, (DateTime?)DateTime.ParseExact(fieldValue, "yyyy-MM-dd", null), null);
    56                     }
    57                 }
    58                 else
    59                     propertyInfo.SetValue(entity, null, null);
    60
    61             }
    62
    63         }
    64         ///<summary>
    65         /// 类型匹配
    66         ///</summary>
    67         ///<param name="type"></param>
    68         ///<param name="typeName"></param>
    69         ///<returns></returns>
    70         publicstaticbool IsType(Type type, string typeName)
    71         {
    72             if (type.ToString() == typeName)
    73                 returntrue;
    74             if (type.ToString() =="System.Object")
    75                 returnfalse;
    76
    77             return IsType(type.BaseType, typeName);
    78         }

  • 相关阅读:
    javaWeb学习总结——文件上传、下载
    基于JDK1.8的JVM 内存结构【JVM篇三】
    Mybatis分页插件PageHelper的学习与使用
    使用IntelliJ IDEA创建第一个Mawen项目
    SpringMVC参数绑定学习总结【前后端数据参数传递】
    【2013年】开发常见问题回顾(一)
    Asp.Net统一前后端提示信息方案
    pip的安装以及binascii报错问题
    win32程序一个简单的计算器
    uva815洪水问题
  • 原文地址:https://www.cnblogs.com/freeliver54/p/3135097.html
Copyright © 2011-2022 走看看