Implementing update operations between objects in C #

Source: Internet
Author: User

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 #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.