This article mainly introduces the use of reflection in C # to traverse an object properties and values of the trick, which should be very useful, this article directly gives the example code, the need for friends can refer to the
Summarize:
An instantiated object TC for a class that iterates through the method (with reflection) that gets all the attributes (Child members):
The code is as follows:
Type t = tc. GetType ()//Get the type of the class
Then use Type.getproperties to get propertyinfo[], and then you can use foreach to traverse the
foreach (PropertyInfo pi in T.getproperties ())
{
Object value1 = Pi. GetValue (TC, NULL));//with PI. GetValue Get Value
String name = Pi. name;//gets the name of the attribute, and then it can be judged by the name to do what you want.
Gets the type of the property, makes a judgment, and then makes a subsequent operation, such as determining that the obtained property is an integer
if (value1. GetType () = = typeof (int))
{
Do the things you want to do
}
}
Attention:
You must set the properties of the get and set methods before reflection gets the property
The code is as follows:
public int Pid
{
get {return PID;}
set {pid = value;}
}