I found a lot of information on the Internet. It can be said that it is similar. I searched a bunch of conceptual things on the Internet. Today I sorted out the reflection items for your use, I promise that I am the most comprehensive thing here, of course, it is also the basic thing. After learning all this, you can learn the specific reflection plug-ins and other applications, so you don't have to read it.
// First, create a class library and generate it as helloworld. dll,
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace webtest
{
Using system;
Namespace webtest
{
Public interface interface1
{
Int add ();
}
Public class reflecttest: interface1
{
Public String write;
Private string writec;
Public String writea
{
Get
{
Return write;
}
Set
{
Write = value;
}
}
Private string writeb
{
Get
{
Return writec;
}
Set
{
Writec = value;
}
}
Public reflecttest ()
{
This. Write = "write ";
This. writec = "writec ";
}
Public reflecttest (string str1, string str2)
{
This. Write = str1;
This. writec = str2;
}
Public String writestring (string S, int B)
{
Return "\ n this is the string printed by the class library function: Welcome," + S + "---" + B ;;
}
Public static string writename (string S)
{
Return "\ n this is the string printed by the class library function: Welcome," + S;
}
Public String writenopara ()
{
Return "\ n this is the string printed by the class library function: you are using a non-parameter method ";
}
Private string writeprivate ()
{
Return "\ n this is the string printed by the class library function: Private type method ";
}
Public int add ()
{
Return 5;
}
}
}
}
// Then, create a project and introduce helloworld. dll,
Using system;
Using system. Threading;
Using system. reflection;
Using webtest. webtest;
Class Test
{
Delegate string testdelegate (string value, int value1 );
Static void main ()
{
// Assembly T = assembly. loadfrom ("webtest. dll"); // same effect as the following
Assembly T = assembly. Load ("webtest ");
Console. Write ("webtest Class Library has the following classes: \ n ");
Foreach (type AAA in T. gettypes ())
{
Console. Write (AAA. Name + "\ n"); // display all classes under this DLL
}
Module [] modules = T. getmodules ();
Console. writeline ();
Foreach (module in modules)
{
Console. writeline ("module name: \ n" + module. Name); // display the module name. This example is "helloworld. dll"
}
Type A = typeof (webtest. webtest. reflecttest); // obtain the type of the specific class and the following effect:
// Type A = T. GetType ("webtest. reflecttest ");
// Console. Write ("classes in the module include:" + A. Name + "\ n ");
String [] BB = {"AAAAA", "bbbbb "};
Object OBJ = activator. createinstance (A, BB); // create an instance of this class. The BB following is a parameter with a constructor.
// Object OBJ = T. createinstance ("webtest. reflecttest"); // same as the preceding Method
// Obtain the set of common instance methods
Methodinfo [] miarr = A. getmethods ();
Console. Write ("\ n common methods: \ n ");
Foreach (methodinfo mi0 in miarr)
{
Console. Write (mi0.name + "\ n"); // display all common methods
}
Methodinfo MI = A. getmethod ("writestring"); // display the specific method
Object [] AA = {"using non-static methods with Parameters", 2 };
String S = (string) MI. Invoke (OBJ, AA); // call a method with Parameters
Methodinfo Mi1 = A. getmethod ("writename ");
String [] aa1 = {"static method used "};
String S1 = (string) mi1.invoke (null, aa1); // call a static method
Methodinfo mi2 = A. getmethod ("writenopara ");
String S2 = (string) mi2.invoke (OBJ, null); // call a method without Parameters
Methodinfo MI3 = A. getmethod ("writeprivate", bindingflags. instance | bindingflags. nonpublic );
String S3 = (string) mi3.invoke (OBJ, null); // call a private Method
// Console. Write (S3 );
// Obtain the set of public attributes of the Instance
Propertyinfo [] piarr = A. getproperties (bindingflags. instance | bindingflags. nonpublic | bindingflags. Public );
Console. writeline ("\ n show all attributes :");
Foreach (propertyinfo PI in piarr)
{
Console. writeline (PI. Name); // display all attributes
}
Propertyinfo PI1 = A. getproperty ("writea ");
// Pi1.setvalue (OBJ, "writea", null );
// Console. Write (pi1.getvalue (OBJ, null ));
Propertyinfo Pi2 = A. getproperty ("writeb", bindingflags. instance | bindingflags. nonpublic | bindingflags. Public );
Pi2.setvalue (OBJ, "writeb", null );
// Console. Write (pi2.getvalue (OBJ, null ));
Fieldinfo fi1 = A. getfield ("write ");
// Console. Write (fi1.getvalue (OBJ ));
// obtain the set of instance public constructors
constructorinfo [] CI1 =. getconstructors ();
console. writeline ("\ n show all constructors:");
foreach (constructorinfo Ci In CI1)
{< br> console. write (CI. tostring () + "\ n"); // obtain the constructor form
}< br> // constructorinfo ASCI =. getconstructor (New Type [] {typeof (string), typeof (string)});
// console. write (ASCI. tostring ();
webtest. webtest. interface1 obj1 = (webtest. webtest. interface1) T. createinstance ("webtest. reflecttest ");
webtest. webtest. reflecttest obj2 = (webtest. webtest. reflecttest) T. createinstance ("webtest. reflecttest ");
// console. write (obj1.add (); typical factory mode
foreach (type TT in T. gettypes ()
{< br> If (TT. getinterface ("interface1 ")! = NULL)
{< br> webtest. webtest. interface1 obj3 = (webtest. webtest. interface1) activator. createinstance (a);
// console. write (obj3.add ();
}< BR >}
Testdelegate method = (testdelegate) Delegate. createdelegate (typeof (testdelegate), OBJ, "writestring ");
// A simple example of dynamically creating a delegate
Console. Write (method ("str1", 2 ));
Console. Read ();
}
}
Here, I have sorted out all the commonly used methods, attributes, and so on. Don't let everyone go about it. Let's just calm down and separate parts by myself, make sure that your reflection learning will get twice the result with half the effort. of course, I will continue to add the method. If you think about it, write it down first.
Not a strong player. It will be in the future.
Http://www.cnblogs.com/leonn/archive/2009/03/11/1408303.html