C # reflection (1)
Yesterday, when I was not familiar with reflection, I thought reflection was mysterious. I went to the Internet to find the answer. Today I found a piece of code and knocked it out! Actually, reflection is that simple!
Reflection is a mechanism through which we can know an unknown type information. for example, there is an object A. This object is not defined by us. It may be captured through the network. It may be defined by generic type, but we want to know the type information of this object, I want to know the methods or attributes of this object. we even want to call the method of this object further. the key is that now we only know that it is an object. We do not know its type, and naturally do not know what methods it has and other information. what should we do at this moment? The reflection mechanism solves such a problem. Through the reflection mechanism, we can know the type information of unknown objects.
For example, we have a DLL file, and we want to call the class in it. now let's assume that the definition and quantity of classes in this DLL file are not fixed and change frequently. maybe you will add a class definition in this DLL one day. maybe you think this is okay. Now the key is that we need to call this DLL in another Assembly. This is what our program must be able to adapt to this DLL change, that is to say, even if the definition of the DLL file is changed, we do not need to change our Assembly. at this time, we will use an unknown DLL. what should we do? Similarly, the reflection mechanism helps us to achieve this through reflection.
To put it bluntly, reflection is to know the type information of the unknown type. There is nothing mysterious to tell!
Today, I will talk about an example of getting assembly information.
Here is an example. the idea of the example is as follows: we have a DLL. this dll contains many classes about motion. each category records a kind of sports information. we need to know the DLL information in another program: (if you still cannot understand what I mean, please follow my steps to change the process !)
Step 1: create a file sport. cs. The content is as follows:
Using system;
Public abstract class sport
{
Protected string name;
Public abstract string getduration ();
Public abstract string getname ();
}
Run the "CSC/T: Library sport. cs" command to compile it.
Step 2: create another file named somesports. CS with the following content:
Using system;
Public class football: Sport
{
Public football ()
{
Name = "football ";
}
Public override string getduration ()
{
Return "Four 15 minute quarters ";
}
Public override string getname ()
{
Return name;
}
}
Public class hockey: Sport
{
Public hockey ()
{
Name = "hockey ";
}
Public override string getduration ()
{
Return "three 20 minute periods ";
}
Public override string getname ()
{
Return name;
}
}
Public class Soccer: Sport
{
Public soccer ()
{
Name = "soccer ";
}
Public override string getduration ()
{
Return "Two 45 minute halves ";
}
Public override string getname ()
{
Return name;
}
}
Run the "CSC/T: Library/R: sport. dll somesports. cs" command to compile the file.
Now we have our motion information DLL file. Now we want to know through the program what classes are there. Please go to the last step:
Step 3: Create the file assemblydemo. cs. The content is as follows:
Using system;
Using system. reflection;
Public class assemblydemo
{
Public static void main (string [] ARGs)
{
Int I, J;
// ==================================
// First the command line arguments are evaluated. If there isn't
// At least one, a usage message is printed
// ========================================
If (ARGs. getlength (0) <1)
{
Console. writeline ("usage is assemblydemo <library_name> ");
}
Else
{
// ======================================
// An assembly object is obtained from the command line argument
// ======================================
Assembly = assembly. loadfrom (ARGs [0]);
Type [] types = assembly. gettypes ();
Console. writeline (assembly. getname (). Name + "contains the following types ");
For (I = 0; I <types. getlength (0); ++ I)
{
Console. writeline ("\ r (" + I + ")" + types [I]. Name );
}
I = types. Length-1;
Console. Write ("make selection (0-" + I + ");");
J = convert. toint32 (console. Readline ());
Console. writeline ();
If (types [J]. issubclassof (typeof (sport )))
{
Constructorinfo CI = types [J]. getconstructor (New Type [0]);
Sport sport = (sport) CI. Invoke (new object [0]);
Console. writeline (sport. getname () + "has" + Sport. getduration ());
}
Else
{
Console. writeline (types [J]. Name + "is not a sub-class of Sport ");
}
}
}
}
}
Run the "CSC/R: sport. dll assemblydemo. cs" command to compile the file.
Next we use "assemblydemo somesports. dll" to run this program.
Further programs require that we enter the option. If we enter 1, the result is displayed: hockeyhasthree 20 minute periods.
Now, I am going to explain how to use the reflection mechanism to access the object type information.
Yesterday, when I was not familiar with reflection, I thought reflection was mysterious. I went to the Internet to find the answer. Today I found a piece of code and knocked it out! Actually, reflection is that simple!
Reflection is a mechanism through which we can know an unknown type information. for example, there is an object A. This object is not defined by us. It may be captured through the network. It may be defined by generic type, but we want to know the type information of this object, I want to know the methods or attributes of this object. we even want to call the method of this object further. the key is that now we only know that it is an object. We do not know its type, and naturally do not know what methods it has and other information. what should we do at this moment? The reflection mechanism solves such a problem. Through the reflection mechanism, we can know the type information of unknown objects.
For example, we have a DLL file, and we want to call the class in it. now let's assume that the definition and quantity of classes in this DLL file are not fixed and change frequently. maybe you will add a class definition in this DLL one day. maybe you think this is okay. Now the key is that we need to call this DLL in another Assembly. This is what our program must be able to adapt to this DLL change, that is to say, even if the definition of the DLL file is changed, we do not need to change our Assembly. at this time, we will use an unknown DLL. what should we do? Similarly, the reflection mechanism helps us to achieve this through reflection.
To put it bluntly, reflection is to know the type information of the unknown type. There is nothing mysterious to tell!
Today, I will talk about an example of getting assembly information.
Here is an example. the idea of the example is as follows: we have a DLL. this dll contains many classes about motion. each category records a kind of sports information. we need to know the DLL information in another program: (if you still cannot understand what I mean, please follow my steps to change the process !)
Step 1: create a file sport. cs. The content is as follows:
Using system;
Public abstract class sport
{
Protected string name;
Public abstract string getduration ();
Public abstract string getname ();
}
Run the "CSC/T: Library sport. cs" command to compile it.
Step 2: create another file named somesports. CS with the following content:
Using system;
Public class football: Sport
{
Public football ()
{
Name = "football ";
}
Public override string getduration ()
{
Return "Four 15 minute quarters ";
}
Public override string getname ()
{
Return name;
}
}
Public class hockey: Sport
{
Public hockey ()
{
Name = "hockey ";
}
Public override string getduration ()
{
Return "three 20 minute periods ";
}
Public override string getname ()
{
Return name;
}
}
Public class Soccer: Sport
{
Public soccer ()
{
Name = "soccer ";
}
Public override string getduration ()
{
Return "Two 45 minute halves ";
}
Public override string getname ()
{
Return name;
}
}
Run the "CSC/T: Library/R: sport. dll somesports. cs" command to compile the file.
Now we have our motion information DLL file. Now we want to know through the program what classes are there. Please go to the last step:
Step 3: Create the file assemblydemo. cs. The content is as follows:
Using system;
Using system. reflection;
Public class assemblydemo
{
Public static void main (string [] ARGs)
{
Int I, J;
// ==================================
// First the command line arguments are evaluated. If there isn't
// At least one, a usage message is printed
// ========================================
If (ARGs. getlength (0) <1)
{
Console. writeline ("usage is assemblydemo <library_name> ");
}
Else
{
// ======================================
// An assembly object is obtained from the command line argument
// ======================================
Assembly = assembly. loadfrom (ARGs [0]);
Type [] types = assembly. gettypes ();
Console. writeline (assembly. getname (). Name + "contains the following types ");
For (I = 0; I <types. getlength (0); ++ I)
{
Console. writeline ("\ r (" + I + ")" + types [I]. Name );
}
I = types. Length-1;
Console. Write ("make selection (0-" + I + ");");
J = convert. toint32 (console. Readline ());
Console. writeline ();
If (types [J]. issubclassof (typeof (sport )))
{
Constructorinfo CI = types [J]. getconstructor (New Type [0]);
Sport sport = (sport) CI. Invoke (new object [0]);
Console. writeline (sport. getname () + "has" + Sport. getduration ());
}
Else
{
Console. writeline (types [J]. Name + "is not a sub-class of Sport ");
}
}
}
}
}
Run the "CSC/R: sport. dll assemblydemo. cs" command to compile the file.
Next we use "assemblydemo somesports. dll" to run this program.
Further programs require that we enter the option. If we enter 1, the result is displayed: hockeyhasthree 20 minute periods.
Now, I am going to explain how to use the reflection mechanism to access the object type information.