// Obtain the type information
// If you call another DLL
// System. reflection. Assembly ASMB = system. reflection. Assembly. loadfrom("DLL name ");
// Type T = ASMB. GetType ("Class Name ");
// If other DLL is not called
System. type T = system. type. GetType ("Class Name ");
Try
{
Object dobj = activator. createinstance (t );
// Obtain method information
System. reflection. methodinfo method = T. getmethod ("method name ");
// Some flag spaces of the call method. This field indicates public and instance method, which is also the default value.
System. reflection. bindingflags flag = system. reflection. bindingflags. Public |
System. reflection. bindingflags. instance;
// Parameters of the getvalue Method
Object [] parameters = new object [] {"parameter 1 "};
// Object returnvalue = method. Invoke (dobj, flag, type. defaultbinder,
Parameters, null );
// Obtain the value returned by the method
Object returnvalue = method. Invoke (dobj, flag, type. defaultbinder,
Parameters, null );
}
Catch (exception ex)
{
}
The following is a reflection class written by myself. You can execute the method based on the class name and method name.
[CSHARP] View plaincopyprint?
-
- UsingSystem;
- UsingSystem. Collections. Generic;
-
- UsingSystem. text;
-
- UsingSystem. reflection;
-
-
- NamespaceSearchfromdb
-
- {
-
- ClassReflection
-
- {
- StringClassname ="";
-
- Type clstype;
-
- PublicReflection (StringClassname)
-
- {
-
- This. Classname = classname;
-
- }
- PublicReflection ()
-
- {
-
-
- }
-
- /// <Summary>
-
- /// Specify the class name when you specify an Instance Object
-
- /// </Summary>
- Public ObjectGetclassinstance (StringAssembly,StringNamespace)
-
- {
-
- // The Assembly isProgramSet Name, namespace is the namespace
-
- Clstype = assembly. Load (assembly). GetType (String. Concat (namespace,".",This. Classname ));
-
- If(Clstype =Null)
-
- Return Null;
- ObjectOBJ = (Object) Activator. createinstance (clstype );
-
- ReturnOBJ;
-
- }
-
- /// <Summary>
-
- /// You do not need to specify the class name for the Instance Object
- /// </Summary>
-
- Public ObjectGetclassinstance (StringAssembly,StringNamespace,StringClassname)
-
- {
-
- Classname = classname;
- Clstype = assembly. Load (assembly). GetType (String. Concat (namespace,".", Classname ));
-
- If(Clstype =Null)
-
- Return Null;
- ObjectOBJ = (Object) Activator. createinstance (clstype );
-
- ReturnOBJ;
-
- }
-
- /// <Summary>
-
- /// Static method of execution class
- /// </Summary>
-
- /// <Param name = "methodname">
-
- /// Method Name of the class
-
- /// </Param>
-
- /// <Param name = "Parameters">
- /// Method parameter type
-
- /// </Param>
-
- /// <Param name = "methodtype">
-
- /// Method parameters
-
- /// </Param>
-
- Public ObjectGetmethod (StringMethodname, type [] methodtype,Object[] Parameters)
-
- {
-
- // Methodtype. setvalue (typeof (string), 1 );
- System. reflection. methodinfo pmethod = clstype. getmethod (methodname, bindingflags. ignorecase | bindingflags. Static | bindingflags. Public,Null, Methodtype,Null);
-
- // Some flag spaces of the call method. This field indicates public and instance method, which is also the default value.
-
- // System. reflection. bindingflags flag = bindingflags. ignorecase | bindingflags. Static | bindingflags. Public;
- ObjectReturnvalue = pmethod. Invoke (Null, Parameters );
-
- // String returnvalue = pmethod. Invoke (clsobj, flag, type. defaultbinder, parameters, null). tostring ();
-
- ReturnReturnvalue;
-
- }
-
- }
- }< br>