您当前的位置: 首页 >  c#

光怪陆离的节日

暂无认证

  • 5浏览

    0关注

    1003博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

光怪陆离的节日 发布时间:2022-10-17 11:11:25 ,浏览量:5

    //通过反射来拷贝对象数据
    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);
                }
            }
        }
    }

在这里插入图片描述 在这里插入图片描述

关注
打赏
1665731445
查看更多评论
立即登录/注册

微信扫码登录

0.0402s