C # Add dynamic attributes to anonymous objects,

Source: Internet
Author: User

C # Add dynamic attributes to anonymous objects,

An object and its dynamic attributes need to be dynamically created during the development process. After trying several methods, I finally completed the requirement, recorded the process, and gave them a reference.

1. dynamically create object 1: anonymous object

Object obj1 = new {Name = "Jin Chao Qian", Age = "31", Birthday = DateTime. Now };

The created anonymous object:

Problem 1: The object attributes cannot be dynamically mapped.

 

Solution: Get object values using reflection

Object obj1 = new {Name = "Jin Chao Qian", Age = "31", Birthday = DateTime. now}; Response. write (string. format ("Name: {0}", obj1.GetType (). getProperty ("Name "). getValue (obj1, null ). toString ()));

Output result

Problem 2: Unable to dynamically create Object Attributes

 

2. Create dynamic objects method 2. Dynamic Objects

Dynamic obj2 = new System. dynamic. expandoObject (); obj2.Name = "Jin Chao Qian"; obj2.Age = 31; obj2.Birthday = DateTime. now; Response. write (string. format ("Name: {0}", obj2.Name ));

Dynamic object created:

Output result:

Problem: still cannot add objects dynamically

 

3. dynamically create objects and their attributes

Dictionary <string, object> temp = new Dictionary <string, object> (); temp. add ("Name", "Jin Chao Qian"); temp ["Age"] = 31; temp ["Birthday"] = DateTime. now; dynamic obj = new System. dynamic. expandoObject (); foreach (KeyValuePair <string, object> item in temp) {(IDictionary <string, object>) obj ). add (item. key, item. value);} Response. write (string. format ("Name: {0}", obj. getType (). getProperty ("name "). getValue (obj, null ). toString ()));

 

Object View:

Output:

 

The output is incorrect and the Object Attributes cannot be obtained using reflection. After investigation, all the fields and properties of this object are null, so we use Dictionary for output as above.

 

Finally, the job is closed. If you have similar needs, please refer to it.

 

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.