zoukankan      html  css  js  c++  java
  • c# 利用反射设置属性值

            /// <summary>
              /// 设置相应属性的值
              /// </summary>
              /// <param name="entity">实体</param>
              /// <param name="fieldName">属性名</param>
              /// <param name="fieldValue">属性值</param>
              public static void SetValue(object entity, string fieldName, string fieldValue)
              {
                  Type entityType = entity.GetType();
     
                 PropertyInfo propertyInfo = entityType.GetProperty(fieldName);
     
                 if (IsType(propertyInfo.PropertyType, "System.String"))
                 {
                     propertyInfo.SetValue(entity, fieldValue, null);
     
                 }
     
                 if (IsType(propertyInfo.PropertyType, "System.Boolean"))
                 {
                     propertyInfo.SetValue(entity, Boolean.Parse(fieldValue), null);
     
                 }
     
                 if (IsType(propertyInfo.PropertyType, "System.Int32"))
                 {
                     if (fieldValue != "")
                         propertyInfo.SetValue(entity, int.Parse(fieldValue), null);
                     else
                         propertyInfo.SetValue(entity, 0, null);
     
                }
     
                 if (IsType(propertyInfo.PropertyType, "System.Decimal"))
                 {
                     if (fieldValue != "")
                         propertyInfo.SetValue(entity, Decimal.Parse(fieldValue), null);
                     else
                         propertyInfo.SetValue(entity, new Decimal(0), null);
     
                 }
     
                 if (IsType(propertyInfo.PropertyType, "System.Nullable`1[System.DateTime]"))
                 {
                     if (fieldValue != "")
                     {
                        try
                         {
                             propertyInfo.SetValue(
                                 entity,
                                 (DateTime?)DateTime.ParseExact(fieldValue, "yyyy-MM-dd HH:mm:ss", null), null);
                         }
                         catch
                         {
                             propertyInfo.SetValue(entity, (DateTime?)DateTime.ParseExact(fieldValue, "yyyy-MM-dd", null), null);
                         }
                     }
                     else
                         propertyInfo.SetValue(entity, null, null);
     
                 }
     
             }
             /// <summary>
             /// 类型匹配
             /// </summary>
             /// <param name="type"></param>
             /// <param name="typeName"></param>
             /// <returns></returns>
             public static bool IsType(Type type, string typeName)
             {
                 if (type.ToString() == typeName)
                     return true;
                 if (type.ToString() == "System.Object")
                     return false;
     
                 return IsType(type.BaseType, typeName);
             }
  • 相关阅读:
    apache启用gzip压缩方法--转载自http://www.cnblogs.com/linzhenjie/archive/2013/03/05/2943635.html
    yii 主从数据库分离-转载http://www.yiichina.com/doc/guide/2.0/db-dao
    服装尺寸
    php 同步因子的并发处理
    NFC会员管理-转载自http://technews.cn/2014/09/13/nfc-sticker/
    Redis 利用锁机制来防止缓存过期产生的惊群现象-转载自 http://my.oschina.net/u/1156660/blog/360552
    移动端H5页面的设计稿尺寸大小规范-转载自http://www.chinaz.com/design/2015/1103/465670.shtml
    服饰行业淘宝商城店铺首页设计报告-转载自http://bbs.paidai.com/topic/88363
    网页设计的标准尺寸
    hdu2099
  • 原文地址:https://www.cnblogs.com/yeagen/p/2662494.html
Copyright © 2011-2022 走看看