A new word is being touched today, which is reflection, and I don't know what it is when I see the word reflex. So I asked the old man, the old man said to me can see a method is Gettipy () method, but what is this thing, through my search for information I am here to publish my own small understanding of it.
One, what is reflection
Of course, for the word reflection, we are not unfamiliar, such as mirrors can reflect, B-ultrasound is also the use of the principle of reflection imaging. But for C #, reflection is the same thing, reflection is an important mechanism in C # that allows you to get information about the members and members of every type in the program or assembly, including classes, structs, delegates, interfaces, enumerations, and so on, at run time. With reflection, you can know each type at your fingertips. In addition, I can create objects directly, even if the type of the object is not known at compile time.
I don't know if anyone can understand through your story, for example, give us a variable that can make an int, or a string, or some other class, but we don't really know this variable at the moment, not just what type of variable it is, if it's a class, We don't know what the methods are in this class, but we can get these things by reflection.
Second, what is the use of reflection
I would like to pass on the above, I should know what the reflection is. But what are the specific uses of reflection, and here are some examples:
1. Use assembly to define and load assemblies, load lists of modules in the assembly manifest, and find types from this assembly and create instances of that type.
2. Using module to understand the assembly that contains the module and the classes in the module, you can also get all the global methods or other specific Non-global methods defined on the module.
3, use ConstructorInfo to understand the name of the constructor, parameters, access modifiers (such as pulic or private) and implementation details (such as abstract or virtual), and so on.
4, use MethodInfo to understand the name of the method, return type, parameters, access modifiers (such as pulic or private) and implementation details (such as abstract or virtual), and so on.
5, use Fiedinfo to understand the name of the field, access modifiers (such as public or private) and implementation details (such as static), and get or set the field value.
6. Add or remove event handlers by using EventInfo to understand the name of the event, event handler data type, custom attribute, declaring type, and reflection type, and so on.
7. Get or set the property value by using PropertyInfo to understand the name, data type, declaring type, reflection type, and read-only or writable state of the property.
8. Use ParameterInfo to understand the name of the parameter, data type, input or output parameters, and the position of the parameter in the method signature.
Iii. namespaces to be used for reflection
In C # to use reflection, which clear space should be quoted. In fact, not a lot of use, just so three: System.Reflection, System.Type, System.Reflection.Assembly. In fact, these three namespaces each have their own usefulness, but the main use is also System.Type class-Through this class can access any given data type of information System.Reflection.Assembly class-It can be used to access information for a given assembly, or to load the assembly into a program. Now let's introduce a namespace called System.Type.
The System.Type class plays a central role in reflection. But it's an abstract base class, type has a derived class that corresponds to each data type, and we use the methods, fields, and properties of the object of this derived class to find all the information about that type.
In fact, there are three representations of the type that gets the given data
String str = "Caosiyuan";
(1) Type t = typeof (str); or type T = typeof (String);
(2) Type t = str. GetType ();
(3) Type T = Type.GetType (str); or Type t = Type.GetType (String);
All three of the above methods are capable of getting string types, but what to do next, such as getting the name of the method inside.
string n = "Grayworm";
Type t = N.gettype ();
foreach (MemberInfo mi in T.getmembers ())
{
Console.WriteLine ("{0}/t{1}", MI.) Membertype,mi. Name);
These methods allow you to obtain the methods provided in a class.
Properties of type
Name Data type names
The fully qualified name of the FullName data type (including the namespace name)
Namespace defines the namespace name of the data type
IsAbstract Indicates whether the type is an abstract type
IsArray Indicates whether the type is an array
Isenum Indicates whether the type is an enumeration
IsClass Indicates whether the type is a class
Isinterface Indicates whether the type is an interface
IsPublic Indicates whether the type is public
IsSealed Indicates whether the type is a sealed class
Isvaluetype Indicates whether the type is a value type
Method of type
GetConstructor (), GetConstructors (): Returns the ConstructorInfo type, which is used to obtain information about the constructor of the class
GetEvent (), GetEvents (): Returns the EventInfo type, which is used to obtain information about the event of the class
GetField (), GetFields (): Returns the FieldInfo type, which is used to obtain information about the field (member variable) of the class
GetInterface (), getinterfaces (): Returns the Interfaceinfo type, which is used to obtain information about the interface implemented by the class
GetMember (), GetMembers (): Returns the MemberInfo type, which is used to obtain information about all members of the class
GetMethod (), GetMethods (): Returns the MethodInfo type, which is used to obtain information about the method of the class
GetProperty (), GetProperties (): Returns the PropertyInfo type, which is used to obtain information about the properties of the class
These members can be invoked by calling the InvokeMember () method of type, or by invoking the Invoke () method of the MethodInfo, PropertyInfo, and other classes.