14 reflection Assembly, 14 reflection

Source: Internet
Author: User

14 reflection Assembly, 14 reflection
1. What is an assembly?Assembly is a concept in. net.Both the dll and exe files in. net are assemblies. Assembly, which can be considered as a package for a bunch of related classes, equivalent to the jar package in Java (*)Assembly includes:Type metadata (each type and member defined in the Code, in binary format), program metadata (assembly list, version number, name, etc), IL code (these are installed in exe or dll), resource files. Each assembly has its own name, version, and other information. The information can be defined in the AssemblyInfo. cs file.  Benefits of using an assembly:Only the required assembly is referenced in the program to reduce the size of the program. An assembly can encapsulate some code and only provide necessary access interfaces.How do I add an Assembly reference?Adding paths, project references, and GAC (Global Assembly Cache) cannot add references cyclically in C # to add references to dll files written in other languages (refer to P/Invoke, in. net call non-assembly dll) extern 2, reflection

Reflection is everywhere and we use it every day. The smart tip of VS is to obtain the class attributes and methods through reflection. The Decompilation tool is also implemented through reflection.

Reflection is the function of dynamically obtaining the Assembly metadata (providing the Assembly type information.

Reflection dynamically obtains the metadata in the assembly for operation.

TypeClass is an important class that implements reflection. Through it, we can obtain all information in the class, including methods and attributes. You can dynamically call tired attributes and methods. Type is the description of the class.

Reflection directly creates an object through. dll and calls members.

You can obtain some information about an object through the type metadata, and instantiate the object call method. This is called "reflection ".

Reflection changes the way objects are created.

The intelligent prompt of the compiler is an application of reflection.

 

3. Type

Use of the Type class

Obtain Type: Type t = typeof (person) through the class)

Obtain the Type: Type t = p. GetType () of the class through the object ()

 

Dynamic Assembly Loading

String path = @ "F: \ MyCSharp \ exercises \ ClassLibrary1 \ bin \ Debug \ ClassLibrary1.dll"; // load Assembly asm = Assembly. LoadFile (path );

 

 

 

Call the GetExportedTypers method of Assembly to obtain all public types defined in Assembly.

Type [] types = asm. GetExportedTypes (); // obtain all public types defined in the dataset. The returned value is an array of types.

 

 

 

Call the GetTpye (name) method of Assembly to obtain the type information defined in Assembly with the full name as name.

// Obtain a specified Type. The "Full name" (fully qualified name: ClassLibrary1.Person) Type typePerson = asm. GetType ("ClassLibrary1.Person") of this Type must be written ");

 

 

4. dynamically create an object

GetConstructor (parameter list); // This is a constructor with parameters found.

 

Activator. CreateInstance (Type t) will dynamically call the class's no-argument constructor to create an object, and the returned value is the created object. If the class does not have a no-argument constructor, an error is reported.

String path = @ "F: \ MyCSharp \ exercises \ ClassLibrary1 \ bin \ Debug \ ClassLibrary1.dll"; // load Assembly asm = Assembly. loadFile (path); // obtain TypeType typeStudent = asm. getType ("ClassLibrary1.Student"); Type typePerson = asm. getType ("ClassLibrary1.Person"); Type typeIFlyable = asm. getType ("ClassLibrary1.IFlyable"); // use CreateInstance (Type t) to create the object student = Activator. createInstance (typeStudent); // The construction function object person = Activator without parameters is called. createInstance (typePerson); // call a constructor without Parameters

 

 

Type method: When writing a program that calls the plug-in, a series of verification is required.

Bool IsAssignableFrom (Type c); determines whether the current Type of variable can accept the value assignment of the c Type variable.

1 // 1. IsAssignableFrom () is to verify whether a type of object can be assigned to another type of variable 2 string path = @ "F: \ MyCSharp \ exercise \ ClassLibrary1 \ bin \ Debug \ ClassLibrary1.dll "; 3 // load Assembly 4 Assembly asm = Assembly. loadFile (path); 5 // obtain Type 6 Type typeStudent = asm. getType ("ClassLibrary1.Student"); 7 Type typePerson = asm. getType ("ClassLibrary1.Person"); 8 Type typeIFlyable = asm. getType ("ClassLibrary1.IFlyable"); 9 // if true is returned, the typeStudnet object can be assigned to the typePerson type 10 Console. writeLine (typePerson. isAssignableFrom (typeStudent); 11 // whether the typeStudent object can be assigned to the typeIFlyable type variable 12 Console. writeLine (typeIFlyable. isAssignableFrom (typePerson); 13 // whether the typeStudent object can be assigned to the typeIFlyable type variable 14 Console. writeLine (typeIFlyable. isAssignableFrom (typeStudent); 15 Console. readKey ();

 

 

 

Bool IsInstanceOfType (object o); judge whether the object o is an instance of the current class (the current class can be an o class, parent class, interface)

1 string path = @ "F: \ MyCSharp \ exercises \ ClassLibrary1 \ bin \ Debug \ ClassLibrary1.dll"; 2 // load Assembly 3 Assembly asm = Assembly. loadFile (path); 4 // obtain a Type of Type 5 Type typeStudent = asm. getType ("ClassLibrary1.Student"); 6 Type typePerson = asm. getType ("ClassLibrary1.Person"); 7 Type typeIFlyable = asm. getType ("ClassLibrary1.IFlyable"); 8 object student = Activator. createInstance (typeStudent); 9 object person = Activator. createInstance (typePerson); 10 // whether the object "person" is the currently specified typePerson object 11 Console. writeLine (typePerson. isInstanceOfType (person); // true12 // whether the student object is the currently specified typePerson object 13 Console. writeLine (typePerson. isInstanceOfType (student); // true14 // whether the object "person" is the currently specified typeStudent object 15 Console. writeLine (typeStudent. isInstanceOfType (person); // false16 // whether the student object is the currently specified typeStudent object 17 Console. writeLine (typeStudent. isInstanceOfType (student); // true18 // whether the object "person" is the currently specified typeIFlyable Object 19 Console. writeLine (typeIFlyable. isInstanceOfType (person); // false20 // whether the student object is the currently specified typeIFlyable object 21 Console. writeLine (typeIFlyable. isInstanceOfType (student); // true22 Console. readKey ();

 

 

Bool IsSubclassOf (Type c); determines whether the current class is a subclass of class c. (Not related to the interface)

1 // 3. IsSubclassOf (Type c) determines whether the current class is a subclass of class c. It is only a class and there is no interface. 2 string path = @ "F: \ MyCSharp \ exercise \ ClassLibrary1 \ bin \ Debug \ ClassLibrary1.dll "; 3 // load Assembly 4 Assembly asm = Assembly. loadFile (path); 5 // obtain Type 6 Type typeStudent = asm. getType ("ClassLibrary1.Student"); 7 Type typePerson = asm. getType ("ClassLibrary1.Person"); 8 Type typeIFlyable = asm. getType ("ClassLibrary1.IFlyable"); 9 // create an object of the specified type 10 object student = Activator. createInstance (typeStudent); 11 object person = Activator. createInstance (typePerson); 12 // judge whether typeStudent is a subclass of typePerson 13 Console. writeLine (typeStudent. isSubclassOf (typePerson); // true14 // judge whether typePerson is a subclass of typeIFlyable 15 Console. writeLine (typePerson. isSubclassOf (typeIFlyable); // false16 // judge whether typeStudent is a subclass of typeIFlyable 17 Console. writeLine (typeStudent. isSubclassOf (typeIFlyable); // false18 Console. readKey ();

 

 

IsAbstract: determines whether it is abstract and contains interfaces.

1 // 4. Check whether IsAbstract is abstract, including interface 2 string path = @ "F: \ MyCSharp \ exercises \ ClassLibrary1 \ bin \ Debug \ ClassLibrary1.dll "; 3 4 // load Assembly 5 Assembly asm = Assembly. loadFile (path); 6 7 // obtain a Type of Type 8 Type typeStudent = asm. getType ("ClassLibrary1.Student"); 9 Type typePerson = asm. getType ("ClassLibrary1.Person"); 10 Type typeIFlyable = asm. getType ("ClassLibrary1.IFlyable"); 11 Type typeMyclass1 = asm. getType ("ClassLibrary1.MyClass1"); 12 Type typeMyclass2 = asm. getType ("ClassLibrary1.MyClass2"); 13 14 // create an object of the specified type 15 object student = Activator. createInstance (typeStudent); 16 object person = Activator. createInstance (typePerson); 17 18 // determines whether it is abstract, including interface 19 Console. writeLine (typeStudent. isAbstract); // false20 Console. writeLine (typePerson. isAbstract); // false21 Console. writeLine (typeIFlyable. isAbstract); // true22 Console. writeLine (typeMyclass1.IsAbstract); // true23 Console. writeLine (typeMyclass2.IsAbstract); // true24 25 Console. readKey ();

 

 

5. Dynamic call Members

The abstract class of the MemberInfo class has many subclasses. All of the following classes are inherited from the MemberInfo class:

PropertyInfo

1 // all properties 2 PropertyInfo [] properties = typePerson. getProperties (); 3 // output the name of each attribute through a loop 4 for (int I = 0; I <properties. length; I ++) 5 {6 // loop output 7 Console. writeLine (properties [I]. name); 8} 9 Console. readKey ();

 

 

Main members:

CanRead, CanWrite, and PropertyType attributes;

SetValue and GetValue: the first parameter for reading and setting values is the instance object, because set and get are for specific instances, and the last parameter is null. PInfo. SetValue (p1, 30, null );

 

How to obtain MethodInfo

 

1 // obtain all methods in the Person Class 2 MethodInfo [] methods = typePerson. getMethods (); 3 // traverse the array of all obtained methods 4 for (int I = 0; I <methods. length; I ++) 5 {6 // name of the method that outputs all methods 7 Console. writeLine (methods [I]. name); 8} 9 Console. readKey ();

 

 

 

 

Obtain a specified Method
  1. 1 // obtain the method of the Person class name SayHi without any parameters. 2. MethodInfo method = typePerson. GetMethod ("SayHi ")

 

Call this method

  1. 1 MethodInfo method = typePerson. GetMethod ("SayHi"); 2 // determines whether the obtained method is null. 3 if (method! = Null) 4 {5 // because the SayHi () method is an instance method rather than a static method, you must call the SayHi () method on an object, so method. invoke () needs to pass a specific Person object into 6 object obj = Activator. createInstance (typePerson); // create a Person object 7 // call the Invoke () method to pass in the object created by person 8 method. invoke (obj, null); // call the SayHi () method. If this method does not have a parameter, the second parameter is null 9} 10 else11 {12 Console. writeLine ("the method you want to call is not understood"); 13} 14 Console. readKey ();

     

Reload Method for calling this method
  1. // Obtain the method named SayHi (the second parameter is the parameter of the corresponding type: typeof (the parameter of the corresponding type) MethodInfo method = typePerson. getMethod ("SayHi", new Type [] {typeof (string), typeof (string)}); if (method! = Null) {// because the SayHi () method is an instance method rather than a static method, you must call the SayHi () method on an object. invoke () needs to pass a specific Person object into object obj = Activator. createInstance (typePerson); // create a Person object method. invoke (obj, new object [] {"Zhang San", "Have you eaten"}); // call the SayHi (string s1, string s2) overload method} else {Console. writeLine ("the method you want to call is not found");} Console. readKey ();

     


 

FieldInfo

  1. 1 // obtain all the fields 2 FieldInfo [] fields = typePerson. getFields (); 3 // traverse each field 4 for (int I = 0; I <fields. length; I ++) 5 {6 // output each field name 7 Console. writeLine (fields [I]. name); 8} 9 Console. readKey ();

     


 

EventInfo acquisition event

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



From Weizhi note (Wiz)



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.