C # reflection (2)
If you have not read C # reflection (1), we recommend that you read C # reflection (1) first. In the previous article, some people commented on the basics of what I wrote. In fact, I also know that I am only writing the most basic syntax. The reason why I write it is that I learned programming for a year or two before I came into contact with reflection, I believe many people will have the same experience with me. Almost 90% of books in the bookstore do not talk about reflection. In addition, Baidu and Goole have searched for reflection, but it is difficult to find the answer they like. I hope that what I wrote will help beginners who are new to programming.
I don't want to describe the concept of reflection too much here. I still use my own simplest and most direct language to describe reflection-"reflection is a mechanism. Through this mechanism, we can know the details of some bit-aware Assembly !"; In the previous article, we learned how to obtain information about an unknown assembly. Next we will talk about how to know the information about an unknown program module:
Module information is accessed through the module class. The following describes how to use the module class through a sub-class. If you are a dedicated programmer, you should understand the module details.
Or continue to use C # reflection (1) class child. Next we will write a new file moduledemo. CS. The content is as follows:
// Compile the command CSC/R: sport. dll moduledemo. CS
Using system;
Using system. reflection;
Public class moduledemo
{
Public static void main (string [] ARGs)
{
// ======================================
// Am module object is obtained representing
// Somesports. dll library file
// ======================================
Assembly = assembly. Load ("somesports ");
Module module = assembly. getmodule ("somesports. dll ");
// ======================================
// Search the module for the Type named "football"
Type [] types = module. findtypes (module. filtertypename, "football ");
If (types. length! = 0)
{
Constructorinfo CI = types [0]. getconstructor (New Type [0]);
Sport sport = (sport) CI. Invoke (new object [0]);
Console. writeline (sport. getname () + "has" + Sport. getduration ());
}
Else
{
Console. writeline ("type not found ");
}
}
}
We compile with CSC/R: sport. dll moduledemo. CS and run the program with mouduledemo to see the following output: football has four 15 minute quarters.
The basic knowledge of C # reflection is to access the type information of unknown objects. In the next article, I will introduce it and introduce an application that I have previously written, data of different types of objects with the same attributes or domain names can be copied to each other (in Java, it can be used for data replication between pojo and form bean in J2EE ).
If you have not read C # reflection (1), we recommend that you read C # reflection (1) first. In the previous article, some people commented on the basics of what I wrote. In fact, I also know that I am only writing the most basic syntax. The reason why I write it is that I learned programming for a year or two before I came into contact with reflection, I believe many people will have the same experience with me. Almost 90% of books in the bookstore do not talk about reflection. In addition, Baidu and Goole have searched for reflection, but it is difficult to find the answer they like. I hope that what I wrote will help beginners who are new to programming.
I don't want to describe the concept of reflection too much here. I still use my own simplest and most direct language to describe reflection-"reflection is a mechanism. Through this mechanism, we can know the details of some bit-aware Assembly !"; In the previous article, we learned how to obtain information about an unknown assembly. Next we will talk about how to know the information about an unknown program module:
Module information is accessed through the module class. The following describes how to use the module class through a sub-class. If you are a dedicated programmer, you should understand the module details.
Or continue to use C # reflection (1) class child. Next we will write a new file moduledemo. CS. The content is as follows:
// Compile the command CSC/R: sport. dll moduledemo. CS
Using system;
Using system. reflection;
Public class moduledemo
{
Public static void main (string [] ARGs)
{
// ======================================
// Am module object is obtained representing
// Somesports. dll library file
// ======================================
Assembly = assembly. Load ("somesports ");
Module module = assembly. getmodule ("somesports. dll ");
// ======================================
// Search the module for the Type named "football"
Type [] types = module. findtypes (module. filtertypename, "football ");
If (types. length! = 0)
{
Constructorinfo CI = types [0]. getconstructor (New Type [0]);
Sport sport = (sport) CI. Invoke (new object [0]);
Console. writeline (sport. getname () + "has" + Sport. getduration ());
}
Else
{
Console. writeline ("type not found ");
}
}
}
We compile with CSC/R: sport. dll moduledemo. CS and run the program with mouduledemo to see the following output: football has four 15 minute quarters.
The basic knowledge of C # reflection is to access the type information of unknown objects. In the next article, I will introduce it and introduce an application that I have previously written, data of different types of objects with the same attributes or domain names can be copied to each other (in Java, it can be used for data replication between pojo and form bean in J2EE ).