C # Introduction to reflection

Source: Internet
Author: User

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.

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.