Guide
1. What is Reflection
2, the cornerstone of reflection--meta-data
3. Viewing meta data with Ildasm.exe
4. Members to be concerned with System.Reflection namespaces
5. How to get the type instance
6. Late binding and System.activator class
What is reflection
In computer science, reflection refers to the ability of a computer program to access, detect, and modify its own state or behavior at runtime (run time). This is an explanation of the wiki. Both C # and Java support reflection, and reflection is heavily applied in mainstream C # and Java frameworks. The main application scenarios for reflection are as follows:
1. Use reflection to dynamic analysis to explore the types in the assembly (Ildasm.exe and Ilspy, and other anti-compilation tools)
2. Late binding
3, Attribute
4. Access private members of a class
5. The CLR performs certain operations by using a reflection mechanism such as: when serializing an object, using reflection, GC using reflection to construct a reference tree, referring to the type of equal () method to compare the corresponding fields with reflection
The cornerstone of reflection-meta-data
The previous section describes the definition of reflection and the main scenarios of reflection. So why do C # and Java have a reflection mechanism? Or is it not that all programming languages have a reflection mechanism? To answer these questions, we have to talk about the cornerstone of reflection-the meta-data.
What is meta data?
Metadata is the data that describes the data (Metadat is data). This definition may be somewhat blurry, and in C #, for example, metadata is a data structure for recording classes, structures, and member variables.
Viewing metadata with Ildasm.exe
In the previous section, the definition of metadata may be ambiguous, and I'll use an example in conjunction with the Ildasm.exe tool to discuss the meta-data
using System; class demolib{ privatestring _name; Public string Name { get{return _name;} Set {_name = value;} } Public void Print () { Console.WriteLine (Name); }}
Csc/t:library DemoLib.cs
Using Ildasm.exe to open the DemoLib.dll you just generated, hold down CTRL +m to get the metadata information for the current assembly.
Let's look at this metainfo.
TypeDef #n, TypeRef #n, Assembly, AssemblyRef #n, User Strings
The 5 places above are what we need to focus on.
TypeDef #n represent type definition n is a positive integer (same as)
TypeRef #n represents a type reference (the type referenced by the current type)
Assembly information for the current assembly
AssemblyRef #n assembly information referenced by the current assembly
User Strings the literal string value used by the current assembly
We mainly look at the content under the TypeDef
Field #1 (04000001) The fields defined in the type
Method #1 (06000001) defined in the type
Property #1 (17000001) the properties defined in the type
The metadata records the type data, which is why the metadata is called the data that describes the data.
Members to follow in the System.Reflection namespace
System.Reflection named controls provide members that are related to the reflection mechanism
Type |
Role |
Assembly |
The abstract class contains a number of static methods through which an assembly can be loaded, understood, and manipulated |
MemberInfo |
The class is an abstract base class that defines public behavior for EventInfo, FieldInfo, MethodInfo, and PropertyInfo types |
MethodInfo |
The abstract class contains information about the given method |
A brief description of some of the processes of reflection
How to get the type instance
It is known that getting the type instance of an object is the core operation of the reflection mechanism. A total of 4 methods for obtaining a type instance are provided in C # and are summarized as follows
1. Use System.Object.GetType () to obtain the type instance
Obviously, to use this method, you must get the compile-time information of the type and have the type instance in memory
2. Use typeof () to obtain the type instance
You need to provide compile-time information for the type, typeof requires a strongly typed name for the type, not text information
3. Use System.Type.GetType () to obtain the Type instance
By calling the System.Type class's Static method GetType () to specify the fully qualified name of the type, get the type instance, in this way we do not need to specify the types information at compile time, and the System.Type.GetType () method has multiple overloaded methods. For more information, see MSDN
4. Use Assmbly.gettype () to obtain the type instance
The fourth is the most common, loading the DLL to get the assembly instance, obtaining the corresponding type information through the assembly instance
Late binding and System.activator class
Late binding is a technique that creates an instance of a given type and invokes its members at run time without needing to know at compile time that it exists. This approach enables our system to scale up
The System.activator class provides a way to create an instance CreateInstance () This method has many overloaded methods see MSDN
Let's look at a reflection instance
usingSystem;namespacexxx.common.lib{ Public classTestLib { PublicTestLib () {Console.WriteLine ("TestLib Ctor."); } Public intSum (intAintb) {Console.WriteLine ("Method testlib.sum () invoked"); returnA +C; } }}
Csc/t:library Lib.cs
Get Lib.dll
We're writing a App.cs. Load the sum method in the Lib.cs by means of reflection
usingSystem;usingSystem.Reflection;classapp{Static voidMain () {Assembly assmbly= Assembly.LoadFrom ("Lib.dll");//LoadFrom ("FileName.dll") Load (FileName)Type type = assmbly. GetType ("XXX.Common.Lib.TestLib"); Objectobj = Activator.CreateInstance (type);//Enter the XXX.Common.Lib.TestLib constructorMethodInfo mi= Type. GetMethod ("Sum"); Object[] Pars =New Object[]{1,2}; intresult =0; Result= (int) mi. Invoke (Obj,pars); Console.WriteLine (result); }}
CSC App.cs
Run results
End of this article
Probe into the source-reflection mechanism of class library