One: Understanding Assemblies
As long as you are dealing with assemblies using VS, we automatically generate assemblies by editing and producing executable programs. So what's the assembly? The DLLs and EXE files in. NET are assemblies (Assembly).
Can be seen as a bundle of related class packages, equivalent to a jar package (*) in Java;
Assembly contains: Type metadata, assembly metadata, IL code, resource file
Type metadata: Describes the binary form of each type and member defined in code.
Assembly Meta Data: assembly manifest, version number, name, etc.;
Il code: is encapsulated in EXE and IL by Microsoft.
Resource files: Each assembly has its own name, version, and other information that can be defined by the AssemblyInfo.cs file itself.
Benefit: The program only references the required assemblies, reducing the size of the program. You can encapsulate a code that provides only the necessary access interfaces.
Second: Reflective Learning
Reflection seems so advanced that it doesn't sound easy to understand, but if we use it often, it becomes very simple.
The smart hint of VS is to get the properties of the class, methods, etc. through reflection, as well as the anti-compilation tool.
Definition: Reflection is the dynamic acquisition of metadata in an assembly to manipulate the type. Reflection is the invocation of a member directly by invoking a. dll to create an object. You get some information about an object through type metadata, and you can also instantiate an object invocation method, which is reflection.
The type class is an important class for implementing reflection, through which you can get all the information in a class, including methods, properties, etc., that can be dynamically called classes, methods, type is a description of the class, and reflection makes the way the object was created changed.
- Assembly stores some of the things we call assemblies, and we use this class to invoke assemblies.
- Use reflection to create an instantiated object by using Activator.CreateInstance (). The object type is returned.
- Through Minfo. Invoke (Object,null) to invoke the method in the class library. The first is the object type, and the second is the argument list of the method we call.
EG1:
Read the Clb.dll file and read all the class (type) inside.
Clb.dll file
namespace clb{public class Class1 { //attribute private int age {get; set;} Method public void Sayhi () { Console.WriteLine ("Hello World?") "); } Interface public interface iflyable { void Fly (); Delegate public delegate void MyDelegate ();} }
Main () method
static void Main (string[] args) { ///through assembly to fetch the local assembly. Assembly asm = assembly.loadfile ("C:/clb.dll"); Note that its return value is type[] type=asm. GetTypes (); Get all the types and note the return value. foreach (type item in type) { Console.WriteLine (item. Name); } Console.read (); }
Gets only the public type of the
Get only public type type[] type = asm. Getexportedtypes ();
An error follows, the workaround is that we have a problem with the setup, in the debug-options and Settings---general (), so that you can read to the local DLL file.
Eg: loads all the methods in the Class1.
Load assembly Assembly asm = assembly.loadfile ("C:/clb.dll"); Get Class1 This type, Typeclass describes the type of the Class1 class, which stores some related information about Class1. Type typeclass= asm. GetType ("Clb.class1"); Get all the methods in Class1, and note the return value. methodinfo[] minfo= typeclass. GetMethods (); foreach (MethodInfo item in Minfo) { Console.WriteLine (item. Name); } Console.read ();
The method in the image below is not in our class, which is a public method of our object base class.
eg
Creates an object by reflection, invoking a method in the class library.
1: Load assembly Assembly asm = assembly.loadfile ("C:/clb.dll"); 2: Get Class1 This type, Typeclass describes the type of the Class1 class, which stores some related information about Class1. Type typeclass= asm. GetType ("Clb.class1"); 3: Get all the methods in Class1, and note the return value. MethodInfo minfo = Typeclass. GetMethod ("Sayhi"); Console.WriteLine (Minfo. Name); Creates an object of type Class1 , obj = Activator.CreateInstance (typeclass); Method Minfo is called by Reflection . Invoke (obj, null); Console.read ();
Third: Use of the Type class
Gets the type through the class, gets the type of the class through the object, and gets the type through the Assembly class.
Person P=new person (); Gets the type type t= p.gettype () through the GetType class of the base class; obtained by TypeOf. Type ts = typeof (person); obtained through an assembly; Assembly asm = assembly.loadfile ("C:/clb.dll"); Type TSS = asm. GetType ("Clb.dll.Person");
30 Days C # foundation consolidates----assemblies, reflection