星空网 > 软件开发 > ASP.net

C#中实现对象间的更新操作

      最近工作的时候遇到一个问题,根据Web端接收到的对象obj1,更新对应的对象值ogj2。先判断obj1中属性值是否为null,

若不等于null,则更新obj2中对应属性值;若等于null,则保持obj2中对应属性值不变。

      先创建Student Class:

C#中实现对象间的更新操作C#中实现对象间的更新操作
 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace Property 7 { 8   public class Student 9   {10     public string Name { get; set; }11 12     public string number { get; set; }13 14     public string School {get;set;}15 16     public string Score { get; set; }17   }18 }

View Code

      方法实现:

C#中实现对象间的更新操作C#中实现对象间的更新操作
 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace Property 7 { 8   class Program 9   {10     static void Main(string[] args)11     {12       Student std1 = new Student();13       std1.Name = "Zhang San";14       std1.number = "1001";15       std1.School = "Jiangnan University";16       std1.Score = "87.98";17 18       Student std2 = new Student();19       std2.Name = "Li Si";20       std2.Score = "98.98";21 22       Console.WriteLine("Before:");23       foreach (System.Reflection.PropertyInfo p in std1.GetType().GetProperties())24       {25         Console.WriteLine("{0}:  {1}", p.Name, p.GetValue(std1,null));26 27       }28 29       foreach (System.Reflection.PropertyInfo p in std2.GetType().GetProperties())30       {31         //Console.WriteLine("{0}:  {1}", p.Name, p.GetValue(std2, null));32         if (p.GetValue(std2, null) != null)33           p.SetValue(std1, p.GetValue(std2, null), null);34       }35       Console.WriteLine("After:");36       foreach (System.Reflection.PropertyInfo p in std1.GetType().GetProperties())37       {38         Console.WriteLine("{0}:  {1}", p.Name, p.GetValue(std1, null));39 40       }41       Console.ReadKey();42 43     }44   }45 }

View Code

        结果:

  C#中实现对象间的更新操作

2016-04-30




原标题:C#中实现对象间的更新操作

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

京华达:https://www.goluckyvip.com/tag/8844.html
中国到泰国快递:https://www.goluckyvip.com/tag/88440.html
泰国快递到中国:https://www.goluckyvip.com/tag/88441.html
广州到泰国货运:https://www.goluckyvip.com/tag/88442.html
中国 泰国物流:https://www.goluckyvip.com/tag/88443.html
国内发泰国物流:https://www.goluckyvip.com/tag/88444.html
TikTok 将推出先买后付服务 :https://www.goluckyvip.com/news/188219.html
深圳有没有比较好玩的景点 深圳有没有比较好玩的景点推荐一下:https://www.vstour.cn/a/366175.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流