Reflection is the dynamic acquisition of metadata in an assembly, creating an object directly from a. dll and invoking the member.
Type is a reflection of an important class. Gets all the information in the class by type, including methods, properties, and so on.
First, the simple use of type
1. Pass the typeof (type name) or class instance. The GetType () method gets the type of one of the types.
2. All properties in a type can be obtained by using the GetFields method.
An instance of the type can be created by 3.activator.createinstance (type).
4.IsInstanceOfType () to determine if the instance is of a certain type.
5.IsAssignableFrom () to determine whether an instance of the latter type can be assigned to the former type.
6.IsSubclassOf () to determine whether an object type is an abstract class, where the abstract class refers to a class that cannot be instantiated.
7.BaseType, gets the type of the parent class.
Second, reflection reflection
1. By loading an assembly with the Assemly.loadfile () static method, the assembly can be thought of as a file that encapsulates many classes.
2.GetTypes gets all the classes in the Assembly, GetType () Gets the class that specifies the class name, which is the full name of the specified class, which is the name of the assembly and the name of the class.
3.GetExportedTypes (), gets all the public classes, this method is not accessible to private classes.
5.GetMehod (), it should be noted that if the search method has overloaded, directly through the method name to search is not found,
This needs to be selected by the second parameter, type type[], if you want to find a method without parameters, defined as new type[]{}, you do not need to write any data.
If there are parameters, they can be written sequentially.
6. Through the MethodInfo invoke () method to execute the search method, here you need to pass in an object to indicate that the type of method, also need to pass in parameters, this method does not exist 1 parameters of the overloaded,
The processing method is the same as the 5th, if not written as new object[]{}.
7.GetProperty () Searches for the specified property, type PropertyInfo.
8.PropertyInfo gets the value of the property by GetValue (), SetValue () Gets the property's.
9. Two ways to create an instance
A.activator.createinstance (), but the Activator property of the System namespace, is not known whether it belongs to a reflection method.
B. By means of a constructor, you can get to the constructor by using the type to obtain a method of the specified types.
The system has inherited the GetConstructor () method, guessing that the interior should also be implemented through GetMethod, so that by performing a search to
constructor to obtain an instance of the corresponding type.
10. Summary
A. In cases where Getmothod and GetProperty are not private members of the class, the overloads of the method indicate that the overloads of the two methods
There are BindingFlags enumeration options, and this enumeration is a flag enumeration, and enumeration members are not mutually exclusive, but are mutually compatible.
They can be used with or without operations to meet the needs of the business. Here, set this enumeration to bindingflags.nonpublic|. BindingFlags.Instance,
Indicates that a search match is a non-public type and instance type, so that private types are added to the search scope.
B. Many methods are overloaded in a common way, such as when executing methods, searching methods, and searching constructors, all require an object of the array type to be passed in.
If there are no parameters, then the direct declaration is not to assign the value.
c. The searched class can be converted to an instance of the original type, and the As method will return null without an error.
D. Reflection is one way to upgrade software online, leaving the "interface" of the assembly read in the initial version, and the assembly needs to be written in the form of a contract.
This way, whenever an assembly is put in, the program starts, reads the DLL file, and makes a corresponding "upgrade" based on its contents.
Assembly asm = assembly.loadfile (@ "G:\test\demo\myLibClass\bin\Debug\myLibClass.dll"); Get all classes type[] types = asm. GetTypes (); for (int i = 0; i < types. Length; i++) {Textbox1.appendtext (types[i]. Name + "\ r \ n"); }//Get all public class type[] T1 = asm. Getexportedtypes (); for (int i = 0; i < t1. Length; i++) {Textbox1.appendtext (T1[i]. Name + "\ r \ n"); }//Gets the specified type required to fill in the full name of the namespace + class name type T2 = asm. GetType ("Mylibclass.student"); Gets a method of type MethodInfo m1 = t2. GetMethod ("Sayhi", new type[] {}); T2. GetMethod ("", BindingFlags.NonPublic | BindingFlags.Instance); T2. GetProperty ("", BindingFlags.NonPublic | BindingFlags.Instance); Object obj = Activator.CreateInstance (t2) based on type creation; String SSS = M1. Invoke (obj, new string[] {}) As String; GetThe constructor for the type ConstructorInfo info = t2. GetConstructor (new type[] {}); Object Stu = info is created by executing the constructor. Invoke (obj, new object[] {}); Gets the property PropertyInfo pinfo = t2. GetProperty ("id"); Setting the value of the property to null means that the property is not indexed simply string id = pinfo. GetValue (obj, null). ToString ();
C # Reflection Reflection