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 .