Cannot get PropertyInfo SetValue to Work for a Structure

This is because a structure is a value type and both SetValue and GetValue
expect Objects causing the arguments to boxed. As a result, the GetValue and
SetValue are working the boxed object rather than on the actual structure.

    object boxedObj = messageReceiveDTO;
       PropertyInfo[] propertys = messageReceiveDTO.GetType().GetProperties();
       foreach (PropertyInfo p in propertys)
       {
         if (p.GetValue(messageReceiveDTO, null) == null || p.GetValue(messageReceiveDTO, null).ToString() == "0")
          {
            continue;
          }
          p.SetValue(boxedObj, DecryptData(p.GetValue(messageReceiveDTO, null).ToString()), null);
       }
       messageReceiveDTO = (MessageReceiveDTO)boxedObj;

将值类型进行装箱,转换为对象,然后再拆箱就可以修改value。因为MessageReceiveDTO用到地方比较多,不适合再把它修改为类,所以就只能用这个方法了。

public string DecryptData(string decryptData)//用于解密数据

若是有更好的方法,求交流....

 
posted @ 2012-09-06 15:32  爱的华尔兹  阅读(191)  评论(0编辑  收藏  举报