Recently work encountered a problem, according to the Web side received the object obj1, update the corresponding object value ogj2. First determine if the property value in Obj1 is null,
If not equal to NULL, the corresponding property value in Obj2 is updated, and if equal to NULL, the corresponding property value in Obj2 is maintained.
First create the student Class:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespace Property7 {8 Public classStudent9 {Ten Public stringName {Get;Set; } One A Public stringNumber {Get;Set; } - - Public stringSchool {Get;Set;} the - Public stringScore {Get;Set; } - } -}View Code
Method implementation:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespace Property7 {8 class Program9 {Ten Static voidMain (string[] args) One { AStudent STD1 =NewStudent (); -Std1. Name ="Zhang San"; -Std1.number ="1001"; theStd1. School ="Jiangnan University"; -Std1. Score ="87.98"; - -Student STD2 =NewStudent (); +Std2. Name ="Li Si"; -Std2. Score ="98.98"; + AConsole.WriteLine ("Before:"); at foreach(System.Reflection.PropertyInfo PinchSTD1. GetType (). GetProperties ()) - { -Console.WriteLine ("{0}: {1}", P.name, P.getvalue (STD1,NULL)); - - } - in foreach(System.Reflection.PropertyInfo PinchSTD2. GetType (). GetProperties ()) - { to //Console.WriteLine ("{0}: {1}", P.name, P.getvalue (STD2, null)); + if(P.getvalue (STD2,NULL) !=NULL) -P.setvalue (STD1, P.getvalue (STD2,NULL),NULL); the } *Console.WriteLine ("After :"); $ foreach(System.Reflection.PropertyInfo PinchSTD1. GetType (). GetProperties ())Panax Notoginseng { -Console.WriteLine ("{0}: {1}", P.name, P.getvalue (STD1,NULL)); the + } A Console.readkey (); the + } - } $}View Code
Results:
2016-04-30
Implementing update operations between objects in C #