// 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 ");