C # reflection usage (non-original)

Source: Internet
Author: User
I searched for a lot of information on the Internet, which can be said to be 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 and, of course, the basic thing,
After learning all this, you can learn the specific reflection plug-ins and other applications.
First, create a class library and generate it as helloworld. dll.

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 "Welcome," + S + "---" + B ;;
}

Public static string writename (string S)
...{
Return "Welcome," + S;
}

Public String writenopara ()
...{
Return "you are using a non-parameter method ";
}

Private string writeprivate ()
...{
Return "Private method ";
}

Public int add ()
...{
Return 5;
}
}
}

Then, create a project and introduce helloworld. dll.

Using system;

Using system. Threading;
Using system. reflection;

Class Test
...{
Delegate string testdelegate (string value, string value1 );
Static void main ()
...{
// Assembly T = assembly. loadfrom ("helloworld. dll"); same effect as below
Assembly T = assembly. Load ("helloworld ");

//************************************** ********************************
Foreach (type AAA in T. gettypes ())
...{
// Console. Write (AAA. Name); // display all classes under the DLL
}

//************************************** ********************************
Module [] modules = T. getmodules ();

Foreach (module in modules)
...{
// Console. writeline ("module name:" + module. Name); // display the module name. This example is "helloworld. dll"
}

//************************************** ********************************
Type A = typeof (webtest. reflecttest); // obtain the type of the specific class and the following effect:
// Type A = T. GetType ("webtest. reflecttest ");//
// Console. Write (A. Name );

//************************************** ********************************
String [] BB =... {"aaaa", "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

//************************************** ********************************
Methodinfo [] miarr = A. getmethods ();
Foreach (methodinfo mi0 in miarr)
...{
// Console. Write (mi0.name); // display all common methods
}

//************************************** ********************************
Methodinfo MI = A. getmethod ("writestring"); // display the specific method
Object [] AA =... {"using a non-static method 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 );

//************************************** ********************************
Propertyinfo [] piarr = A. getproperties (bindingflags. instance | bindingflags. nonpublic | bindingflags. Public );
Foreach (propertyinfo PI in piarr)
...{
// Console. Write (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 ));

//************************************** ********************************
Constructorinfo [] CI1 = A. getconstructors ();
Foreach (constructorinfo Ci In CI1)
...{
// Console. Write (CI. tostring (); // obtain the constructor form
}

Constructorinfo ASCI = A. getconstructor (New Type []... {typeof (string), typeof (string )});
// Console. Write (ASCI. tostring ());

//************************************** ********************************
Webtest. interface1 obj1 = (webtest. interface1) T. createinstance ("webtest. reflecttest ");
Webtest. reflecttest obj2 = (webtest. reflecttest) T. createinstance ("webtest. reflecttest ");
// Console. Write (obj1.add (); typical factory Mode

//************************************** ********************************
Foreach (type TT in T. gettypes ())
...{
If (TT. getinterface ("interface1 ")! = NULL)
...{
Webtest. interface1 obj3 = (webtest. interface1) activator. createinstance ();
// Console. Write (obj3.add ());
}
}

//************************************** ********************************
Testdelegate method = (testdelegate) Delegate. createdelegate (typeof (testdelegate), OBJ, "writestring ");
// A simple example of dynamically creating a delegate
// Console. Write (method ("str1", 2 ));

//************************************** ********************************
Constructorinfo asci1 = A. getconstructor (New Type [0]);
Webtest. reflecttest obj5 = (webtest. reflecttest) asci1.invoke (null );
// Method for instantiating a function without Parameters
// Console. Write (obj5.writea );

Constructorinfo asci2 = A. getconstructor (New Type []... {typeof (string), typeof (string )});
// Method for instantiating a constructor with Parameters
Webtest. reflecttest obj6 = (webtest. reflecttest) asci2.invoke (bb );
Console. Write (obj6.writea );
//************************************** ********************************

Console. Read ();
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.