zoukankan      html  css  js  c++  java
  • 对象属性很多 反射的机制 拷贝对象

    对于属性很多的对象操作起来却相当麻烦,所以可以采用反射的机制,对每一个属性进行赋值,具体代码如下。
    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);
         }
        }
       }
      }
  • 相关阅读:
    go包初始化顺序
    go map
    go包管理
    C++ 线程池
    RAFT共识算法笔记
    最大子序列和
    常见网络攻击及其防御
    go常用标准库功能
    using代替typedef
    typename和class的区别
  • 原文地址:https://www.cnblogs.com/winner/p/1246206.html
Copyright © 2011-2022 走看看