"Go" C # class reflects simple actions

Source: Internet
Author: User

Ext.: http://www.jb51.net/article/25863.htm

First, set up a test class

Copy CodeThe code is as follows:
public class MyClass
{
public int one {set; get;}
public int, {set; get;}
public int five {set; get;}
public int three {set; get;}
Public int. four {set; get;}
}


Then write the code that reflects the class

Copy CodeThe code is as follows:
MyClass obj = new MyClass ();
Type t = typeof (MyClass);
Cyclic assignment
int i = 0;
foreach (var item in t.getproperties ())
{
Item. SetValue (obj, I, null);
i + = 1;
}
assigning values individually
T.getproperty ("Five"). SetValue (obj, 11111111, null);
Loop get
StringBuilder sb = new StringBuilder ();
foreach (var item in t.getproperties ())
{
Sb. Append ("type:" + item.) Propertytype.fullname + "Property name:" + Item. Name + "Value:" + Item. GetValue (obj, null) + "<br/>");
}
Individual values
int five = Convert.ToInt32 (T.getproperty ("Five"). GetValue (obj, null));
Sb. Append ("The value of the five is taken separately:" + five);
string result = sb. ToString ();
Response.Write (Result);


Test Display results:
Type: System.Int32 property name: One value: 0
Type: System.Int32 property name: Two value: 1
Type: System.Int32 property name: Five value: 11111111
Type: System.Int32 property name: Three value: 3
Type: System.Int32 property name: Four value: 4
The value of five is taken separately: 11111111

"Go" C # class reflects simple actions

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.