Quickly assign values to the Model object using reflection, and assign values to the model object Using Reflection

Source: Internet
Author: User

[Switch] quickly assign values to the Model object using reflection, and assign values to the model object using reflection.

Address: http://blog.csdn.net/gxiangzi/article/details/8629064

Imagine a Business Requirement: there is a contract Table, which involves a large number of fields. Now, the original contract information must be archived for each change of the contract, and the version number increases sequentially. In this case, we need to create a new contract history table with the same fields as the original contract table, and an additional contract version number field. How can I insert original contract information into the contract History Table during archiving?

A solution that is easy to think:

Insert into contract History Table (Field 1, Field 2, Field 3 ............ Field 120, Version Number) select Field 1, Field 2, Field 3 ............ Field, 0 as version from contract table where contract ID = 'xxxxxxx'

In this way, our functions can be implemented, but have you seen it? Because too many fields exist in the table, the SQL statement looks very elegant and the correspondence between fields is prone to problems. Think of the previous example of assigning values to the model Using Reflection to come up with the following solution:

Now, assume that the two entities class1 and class2 have only one newid field more than class1, And the other fields are identical. The simple definition is as follows:

class Class1      {          private string id;            public string Id          {              get { return id; }              set { id = value; }          }          private string name;            public string Name          {              get { return name; }              set { name = value; }          }          private int age;            public int Age          {              get { return age; }              set { age = value; }          }          private int num;            public int Num          {              get { return num; }              set { num = value; }          }        }  
class Class2      {          private string newid;            public string Newid          {              get { return newid; }              set { newid = value; }          }            private string id;            public string Id          {              get { return id; }              set { id = value; }          }          private string name;            public string Name          {              get { return name; }              set { name = value; }          }          private int age;            public int Age          {              get { return age; }              set { age = value; }          }          private int num;            public int Num          {              get { return num; }              set { num = value; }          }      }  

Next we will assign a value to class1, then obtain the attributes of class2 through reflection, and assign the corresponding value of class1 to class2 cyclically. In case of more fields of class2, We will skip it after hand processing. The simple code is as follows:

Class1 c1 = new Class1();           c1.Id = "001";           c1.Name = "ben.jiang";           c1.Num = 712104195;           c1.Age = 24;             Class2 c2 = new Class2();           Type t2 = typeof(Class2);           PropertyInfo[] propertys2 = t2.GetProperties();             Type t1 = typeof(Class1);           PropertyInfo[] propertys1 = t1.GetProperties();             foreach (PropertyInfo pi in propertys2)           {               string name = pi.Name;               if (name == "Newid")               {                   c2.Newid = "newid";                   continue;               }                 object value=t1.GetProperty(name).GetValue(c1, null);               t2.GetProperty(name).SetValue(c2,value ,null);           }  

In this way, the code looks a little more elegant, and it is convenient to process different fields.

Related Article

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.