Reflection (C # programming guide)

Source: Internet
Author: User
Reflection (C # programming guide)

Reflection provides EncapsulationProgramSet, module, and type object (type ). You can use reflection to dynamically create instances of the type, bind the type to an existing object, or obtain the type from the existing object and call its method or access its fields and properties. IfCodeYou can use reflection to access attributes. For more information, see properties.

The following is a static methodGetType-- FromObjectAll types derived from the base class inherit this method-get a simple reflection example of the variable type:

C # copy code
 
// Using GetType to obtain type information:IntI = 42; system. Type type = I. GetType (); system. Console. writeline (type );

Output:

System. int32

This example uses reflection to obtain the complete name of the loaded assembly:

C # copy code
// Using Reflection to get information from an assembly:System. reflection. Assembly o = system. reflection. Assembly. Load ("Mscorlib. dll"); System. Console. writeline (O. getname ());

Output:

Mscorlib, version = 2.0.0.0, culture = neutral, publickeytoken = b77a5c561934e089

Reflection Overview

Reflection is useful in the following scenarios:

    • you need to access the properties of the program metadata. See the topic Use reflection access attributes .

    • checks and instantiates types in an assembly.

    • Create a New Type at runtime. Use system. reflection. emit class.

    • perform post-binding, access the method of the type created at run time. See the topic dynamic loading and usage types .

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.