C#通过反射来拷贝对象数据

    //通过反射来拷贝对象数据
    public static void CopyValue(object origin, object target)
    {
        //System.Reflection.PropertyInfo[] properties = (target.GetType()).GetProperties();
        System.Reflection.FieldInfo[] fields = (origin.GetType()).GetFields();
        System.Reflection.FieldInfo[] fields_target = (target.GetType()).GetFields();
        string s1 = target.GetType().ToString();
        for (int i = 0; i < fields.Length; i++)
        {
            for (int j = 0; j < fields_target.Length; j++)
            {
                if (fields[i].Name == fields_target[j].Name )
                {
                    fields_target[j].SetValue(target, fields[i].GetValue(origin));
                    //properties[j].SetValue(target, fields[i].GetValue(origin), null);
                }
            }
        }
    }

C#通过反射来拷贝对象数据_第1张图片
C#通过反射来拷贝对象数据_第2张图片

你可能感兴趣的:(C#,c#,java,开发语言)