c#,利用反射设置属性值

代码
 1  ///   <summary>
 2           ///  设置相应属性的值
 3           ///   </summary>
 4           ///   <param name="entity"> 实体 </param>
 5           ///   <param name="fieldName"> 属性名 </param>
 6           ///   <param name="fieldValue"> 属性值 </param>
 7           public   static   void  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           public   static   bool  IsType( Type  type,  string  typeName)
71          {
72               if  (type.ToString()  ==  typeName)
73                   return   true ;
74               if  (type.ToString()  ==   " System.Object " )
75                   return   false ;
76 
77               return  IsType(type.BaseType, typeName);
78          }

 

转载于:https://www.cnblogs.com/xtechnet/archive/2010/01/29/1659325.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值