• 对象属性很多 反射的机制 拷贝对象


    对于属性很多的对象操作起来却相当麻烦,所以可以采用反射的机制,对每一个属性进行赋值,具体代码如下。
    public static void CopyValue(object origin,object target)
      {
       System.Reflection.PropertyInfo[] properties = (target.GetType()).GetProperties();
       System.Reflection.FieldInfo[] fields = (origin.GetType()).GetFields();
       for ( int i=0; i< fields.Length; i++)
       {
        for ( int j=0; j< properties.Length; j++)
        {
         if (fields[i].Name == properties[j].Name && properties[j].CanWrite)
         {
          properties[j].SetValue(target,fields[i].GetValue(origin),null);
         }
        }
       }
      }
  • 相关阅读:
    周总结(第十一周)
    周总结(第十周)
    周总结(第九周)
    周总结(第八周)
    周总结(第七周)
    周总结(第六周)
    周总结(第5周)
    周总结(第四周)
    周记
    补周记
  • 原文地址:https://www.cnblogs.com/winner/p/1246206.html
Copyright © 2020-2023  润新知