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