• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
hambert
哪里有抱怨,哪里就有商机
博客园    首页    新随笔    联系   管理    订阅  订阅

反射解决类的复制

      假定一个类,类名是EtyBase,另一个类类名是EtyTwo,EtyTwo继承自EtyBase。现在要求EtyTwo的属性值从一个EtyBase中复制过来传统做法是

View Code
        public void CopyEty(EtyBase from, EtyBase to)
        {
            to.AccStatus = from.AccStatus;
            to.Alarm = from.Alarm;
            to.AlarmType = from.AlarmType;
            to.CarNum = from.CarNum;
            to.DevNum = from.DevNum;
            to.DeviceNum = from.DeviceNum;
            to.Direct = from.Direct;
            to.DriveCode = from.DriveCode;
            to.GpsData = from.GpsData;
            to.GpsEnvent = from.GpsEnvent;
            to.GpsOdo = from.GpsOdo;
            to.GpsSpeed = from.GpsSpeed;
            to.GpsStatus = from.GpsStatus;
            to.GpsrevTime = from.GpsrevTime;
            to.Gsmci = from.Gsmci;
            to.Gsmci1 = from.Gsmci1;
            to.Gsmloc = from.Gsmloc;
            to.Gsmloc1 = from.Gsmloc1;
            to.IsEffective = from.IsEffective;
            to.IsJump = from.IsJump;
            to.IsReply = from.IsReply;
            to.Latitude = from.Latitude;
            to.LaunchStatus = from.LaunchStatus;
            to.Longitude = from.Longitude;
            to.MsgContent = from.MsgContent;
            to.MsgExId = from.MsgExId;
            to.MsgId = from.MsgId;
            to.MsgLength = from.MsgLength;
            to.MsgType = from.MsgType;
            to.NowOverArea = from.NowOverArea;
            to.NowStatus = from.NowStatus;
            to.Oil = from.Oil;
            to.PulseCount = from.PulseCount;
            to.PulseOdo = from.PulseOdo;
            to.PulseSpeed = from.PulseSpeed;
            to.ReplyContent = from.ReplyContent;
            to.RevMsg = from.RevMsg;
            to.Speed = from.Speed;
            to.Status = from.Status;
            to.Temperture = from.Temperture;
            to.UserName = from.UserName;
        }

      这样子做有几点不好的地方

  1. EtyBase的属性改变时复制的属性也得改变,耦合较高;
  2. 若EtyBase的属性比较多,那么这个复制方法将显得比较冗长,写的人手累。

 

      如果用反射来做,我是这么做的

View Code
        public void CopyEty(EtyBase from, EtyBase to)
        {
            //利用反射获得类成员
            FieldInfo[] fieldFroms = from.GetType().GetFields();
            FieldInfo[] fieldTos = to.GetType().GetFields();
            int lenTo = fieldTos.Length;

            for (int i = 0, l = fieldFroms.Length; i < l; i++)
            {
                for (int j = 0; j < lenTo; j++)
                {
                    if (fieldTos[j].Name != fieldFroms[i].Name) continue;
                    fieldTos[j].SetValue(to, fieldFroms[i].GetValue(from));
                    break;
                }
            }
        }

      反射可以解决上述的两个缺点,当类属性改变或增加时,此复制方法无需改变。当然这是要付出些许运行效率的。

posted @ 2013-05-06 17:59  hambert  阅读(590)  评论(1)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3