. Net obtains the attributes, methods, and members of a class, and obtains the attribute values through the attribute name. net attribute values
// Custom class model m = new model ();
// Obtain the Type instance of the class // Type t = typeof (model );
// Obtain m's Type instance Type t = m. GetType ();
String className = t. Name + "\ r \ n ";
// Obtain all methods System. Reflection. MethodInfo [] methods = t. GetMethods ();
// Obtain all the member System. Reflection. MemberInfo [] members = t. GetMembers ();
// Obtain all attributes System. Reflection. PropertyInfo [] properties = t. GetProperties ();
// Obtain the class property name and the attribute value foreach (System. reflection. propertyInfo s int. getProperties () {className + = s. name + ":" + m. getType (). getProperty (s. name ). getValue (m, null ). toString () + "\ r \ n ";}
C ++ traverses all attribute names of a class and obtains the values of all attributes of the class's instances.
The reflection mechanism of. NET is very distinctive. It is supported by VB, C #, and VC languages.
Through the reflection mechanism, you can easily enumerate various information of a class. In addition to attributes, you can also obtain various information such as constructors, methods, and fields. The types can be public or non-public, instantiated and static attributes and methods.
Here is a piece of VC. NET instance code:
Using namespace System;
Using namespace System: Reflection;
// Define the class Student for testing
Public ref class Student
{
Private:
String ^ name;
Int32 number;
String ^ address;
Public:
Student (String ^ name, String ^ address, Int32 number)
{
This-> name = name;
This-> address = address;
This-> number = number;
}
Property String ^ Name
{
String ^ get ()
{
Return name;
}
Void set (String ^ value)
{
Name = value;
}
}
Property Int32 Number
{
Int32 get ()
{
Return number;
}
Void set (Int32 value)
{
Number = value;
}
}
Protected:
Property String ^ Address
{
String ^ get ()
{
Return address;
}
Void set (String ^ value)
{
Address = value;
}
}
};
Void DisplayPropertyInfo (Student ^ student, array <PropertyInfo ^> ^ properties );
Int main (array <System: String ^> ^ args)
{
Student ^ student = gcnew Student ("Alice", "No.193, St. Robinson, NY, USA", 1 );
Student-> Name = "Alice. Caley ";
Student-> Number = 2;
Console: WriteLine (student-> Name );
Console: WriteLine (student-> Number );
Console: WriteLine ();
// Reflection
Type ^ type = student-> GetType ();
// Public instance attributes
Array <PropertyInfo ^> ^ publicProperties = type-> GetProperties (static_cast <BindingFlags> (BindingFlags: Public | BindingFlags: Instance ));
Console: WriteLine ("[Display public property info]");
DisplayPropertyInfo (student, publicProperties );
Cons ...... remaining full text>
For objects, properties, and methods in net
Nodes is an attribute of the treeview object, but this attribute is a set that contains many elements. Each element is a treenode type object.
This statement adds node1, A treenode object, to its nodes set.