Reflection is a mechanism,Through this mechanism, we can know the type information of an unknown type.. For example, its class name and public attribute name. You can also obtain and set attributes of this object.
Some examples:
1. Get class information based on objects
Type mytype = record. GetType ();
2. Get attribute information from class information
Get all attributes:Propertyinfo [] propers = mytype. getproperties ();
The access attribute name is as follows:Propers [0]. Name
You can also obtain a specified property:Mytype. getproperty (proname)
3. Obtain the value of an attribute of the current object.
Mytype. getproperty (propers [0]. Name). getvalue (record, null)
WhereRecordIs the current object.
4. Set the value of an attribute of the current object.
Mytype. getproperty (propers [0]. Name). setvalue (record, propers [0]. Name, null );
WhereRecordIs the current object.
5Obtain class information based on the class name
Type mytype = type. GetType (classname );//Use the fully qualified name of the class
6Generate class instances (objects) based on class information)
Object myobject = activator. createinstance (mytype );//Generate object
Of course there are still many operations.
Reflection is a mechanism,Through this mechanism, we can know the type information of an unknown type.. For example, its class name and public attribute name. You can also obtain and set attributes of this object.
Some examples:
1. Get class information based on objects
Type mytype = record. GetType ();
2. Get attribute information from class information
Get all attributes:Propertyinfo [] propers = mytype. getproperties ();
The access attribute name is as follows:Propers [0]. Name
You can also obtain a specified property:Mytype. getproperty (proname)
3. Obtain the value of an attribute of the current object.
Mytype. getproperty (propers [0]. Name). getvalue (record, null)
WhereRecordIs the current object.
4. Set the value of an attribute of the current object.
Mytype. getproperty (propers [0]. Name). setvalue (record, propers [0]. Name, null );
WhereRecordIs the current object.
5Obtain class information based on the class name
Type mytype = type. GetType (classname );//Use the fully qualified name of the class
6Generate class instances (objects) based on class information)
Object myobject = activator. createinstance (mytype );//Generate object
Of course there are still many operations.