Common reflection methods and Delegation

Source: Internet
Author: User

 

// Obtain the type information

Type T = type. GetType ("testspace. testclass ");
// Parameters of the constructor
Object [] constuctparms = new object [] {"tietong "};
// Create an object based on the Type
Object dobj = activator. createinstance (T, constuctparms );
// Obtain method information
Methodinfo method = T. getmethod ("getvalue ");
// Some flag spaces of the call method. This field indicates public and instance method, which is also the default value.
Bindingflags flag = bindingflags. Public | bindingflags. instance;
// Parameters of the getvalue Method
Object [] parameters = new object [] {"hello "};
// Call a method and use an object to receive the returned value

Object returnvalue = method. Invoke (dobj, flag, type. defaultbinder, parameters, null );

 

------------

Ass = System. reflection. Assembly. LoadFile ( @" D: \ testreflect. dll " );
  Type = Ass. GetType ( " Webtest. reflecttest " ); // Must use namespace + class name
System. reflection. methodinfo Method = Type. getmethod ( " Writestring " ); // Method Name
OBJ = Ass. createinstance ( " Webtest. reflecttest " ); // Must use namespace + class name
String S = ( String ) Method. Invoke (OBJ, New String [] {"Jianglijun"} ); // Instance method call

 

 

Dynamically create Delegation
Delegate isC #Sometimes it is inevitable to dynamically create a delegate. In fact, the delegate is also a type: system. Delegate, all delegates are derived from this class.
System. Delegate provides some static methods to dynamically create a delegate, such as a delegate:

namespace testspace {
delegate string testdelegate (string value);
public class testclass {
Public testclass () {
}< br> Public void getvalue (string value) {
return value;
}< BR >}

Example:
Testclass OBJ = new testclass ();

// Obtain the type. In fact, you can use typeof to obtain the type.
Type T = type. GetType ("testspace. testclass ");
// Create a proxy, input the type, object for creating the proxy, and method name
Testdelegate method = (testdelegate) Delegate. createdelegate (T, OBJ, "getvalue ");

String returnvalue = method ("hello ");

 

Related Article

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.