The. NET reflection mechanism provides an alternative solution for creating objects and calling other methods. For example Code Flexibility. But the problem is that we need to write more code for implementation.
Reflection mechanisms have some disadvantages. The biggest drawback is that the compiler cannot perform type checks on objects. In this case, intelligent sensing of the IDE is powerless. But where is his real advantage? It provides a means to delay the specified class to the runtime.
Step 4 of calling a method using the reflection mechanism:
1 Load Program Set
2. Obtain the class type.
3. Create an instance of this type
4. Method for calling this instance
The system. reflection. Assembly class has two static methods: Assembly. Load (string assemblyname) and Assembly. loadfrom (string filename) to load the assembly to the application's sequential domain.
PS: In. When an object is created in. net, what happened behind the scenes? When we run an application,. Net CLR first creates an application domain to accommodate the application, and then loads the referenced assembly into the application domain. Mscorlib. dll is an assembly that contains many system namespaces and their sub-namespaces including system, system. text, and system. Io. This assembly. The CLR then loads the Assembly to which the running application belongs.
Demo:
(1) namespace classlibrarysport
{
Public Abstract Class Sport
{
Protected StringName;
Public Abstract StringGetname ();
Public Abstract StringGetduration ();
}
}
========================================================== ==============
(2) namespace classlibrarysomesports//This project has been referenced to (1 ).
{
Public Class Football: Classlibrarysport.Sport
{
PublicFootball ()
{
Name ="Football";
}
Public Override StringGetname ()
{
ReturnName;
}
Public Override StringGetduration ()
{
Return "Four 15 minute quarters";
}
}
}
========================================================== ==============
(3) namespace consoleassemblytest//This project has been referenced to (1 ).
{
Class Program
{
Static VoidMain (String[] ARGs)
{
AssemblyAssembly =Assembly. Loadfrom (@ "E:" classlibrarysomesports"
Bin "debug" classlibrarysomesports. dll");
Type[] Types = assembly. gettypes ();
Console. Writeline ("Get type from classlibrarysomesports. dll :");
For(IntI = 0; I <types. length; I ++)
{
Console. Writeline (types [I]. Name );
}
//Use the getconstructor () method to obtain the constructor of the corresponding type and construct the object of this type.
Console. Writeline ("Use method getconstructor ():");
ConstructorinfoCi = types [0]. getconstructor (New Type[0]);
Classlibrarysport.SportSport = (classlibrarysport.Sport) CI. Invoke (New Object[0]);
Console. Writeline (sport. getname () +"Has"+ Sport. getduration ());
//Use the activator. createinstance () method to construct an object of this type.
//Use assembly. createinstance () to return NULL ,??
Console. Writeline ("Use method createinstance ():");
Classlibrarysport.SportSport1 = (classlibrarysport.Sport)
Activator. Createinstance (types [0]);
Console. Writeline (sport1.getname () +"Has"+ Sport1.getduration ());
//The method named "getduration" in the specified type of reflection is executed using the invoke () method.
ObjectObjsport =Activator. Createinstance (types [0]);
MethodinfoMethod = types [0]. getmethod ("Getduration");
ObjectO = method. Invoke (objsport,New Object[0]);
Console. Writeline (oAs String);
Console. Read ();
}
}
}
========================================================== ==============
Output:
Get type from classlibrarysomesports. dll:
Football
Use method getconstructor ():
Football has four 15 minute quarters
Use method createinstance ():
Football has four 15 minute quarters
Four 15 minute quarters