I am from the reflection process of Java. I want to know the reflection process of C #, so I wrote a Summary of the knowledge, hoping to help later.
Reflection provides a descriptionProgramSet, 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. IfCodeUsing features, you can use reflection to access them.
The self-built class libraries for reflection testing are as follows: class car and class deep
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace classlibrary1 {class deep: Car {private string name; Public void run () {system. console. writeline ("deep run method ");}}}
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Namespace Classlibrary1 { Public Class Car { Private String Name; Public Int I; Public String Name { Get { Return Name ;} Set {Name = Value ;}} Public Void Run () {system. Console. writeline ( " Car run Method " );} Public String Myreturnmethod ( String ABC) {system. Console. writeline ( " Car run myreturnmethod " ); Return ABC ;} // Protected void run () // { // System. Console. writeline ("car run method "); // } }}
- You can use the Assembly class to load the Assembly, browse the metadata and components of the assembly, discover types contained in the Assembly, and create instances of these types.
Many members of the Assembly class provide information about the assembly.
- The getname method returns an assemblyname object that provides access to the display name of the Assembly.
- The getcustomattributes method lists attributes applied to an assembly.
- The getfiles method provides access to files in the assembly list.
- The getmanifestresourcenames method provides the name of the resource in the assembly list.
1.1 to obtain an array of Assembly objects (indicating the Assembly currently loaded into the application domain (for example, the default application domain of a simple project), you can use appdomain .. ::. getassemblies method.
Appdomain currentdomain = appdomain. currentdomain; // obtain the current application domain of the current thread. Assembly [] assems = currentdomain. getassemblies (); // gets the Assembly that has been loaded into the execution context of this application domain. Console. writeline ("List of assemblies loaded in current appdomain:"); foreach (Assembly assem in assems) {console. writeline (assem. tostring ());}
1.2 The recommended method for loading an assembly is to use the load method, which identifies the assembly to be loaded by its display name
// Call a DLL other than this program. Note that classlibrary1 is the class library file I wrote.
Assembly assem = assembly. Load ( " Classlibrary1, version = 1.0.0.0, culture = neutral, publickeytoken = NULL " ); // Or use the following statement to replace the previous statement. // Assembly assem = assembly. loadfrom ("classlibrary1.dll "); Console. Write (assem. fullname); assemblyname assemname = Assem. getname (); console. writeline ( " \ Nname: {0} " , Assemname. Name); console. writeline ( " Version: {0}. {1} " , Assemname. version. Major, assemname. version. Minor ); /// View All class names under the class library Int I, J; Type [] types = Assem. gettypes (); // Get class name For (I = 0 ; I <types. getlength ( 0 ); ++ I) {console. writeline (types [I]. Name );}
- 2. Use fieldinfo to find the following information: field name, access modifier (such as public or private), implementation details (such as static), and so on; and obtain or set the field value.
Here is a simple example of getting the first class in this class library.
Type mytype = types [ 0 ]; Fieldinfo [] myfieldinfo; myfieldinfo = Mytype. getfields (bindingflags. nonpublic | Bindingflags. Instance | Bindingflags. Public ); For (I = 0 ; I <myfieldinfo. length; I ++ ) {Console. writeline ( " \ Nname: {0} " , Myfieldinfo [I]. Name); console. writeline ( " Declaring type: {0} " , Myfieldinfo [I]. declaringtype); console. writeline ( " Ispublic: {0} " , Myfieldinfo [I]. ispublic); console. writeline ( " Membertype: {0} " , Myfieldinfo [I]. membertype); console. writeline ( " Fieldtype: {0} " , Myfieldinfo [I]. fieldtype); console. writeline ( " Isfamily: {0} " , Myfieldinfo [I]. isfamily );}