Instantiate the class based on the class name string and call the method or function of the class to convert

Source: Internet
Author: User

// 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?
  1. UsingSystem;
  2. UsingSystem. Collections. Generic;
  3. UsingSystem. text;
  4. UsingSystem. reflection;
  5. NamespaceSearchfromdb
  6. {
  7. ClassReflection
  8. {
  9. StringClassname ="";
  10. Type clstype;
  11. PublicReflection (StringClassname)
  12. {
  13. This. Classname = classname;
  14. }
  15. PublicReflection ()
  16. {
  17. }
  18. /// <Summary>
  19. /// Specify the class name when you specify an Instance Object
  20. /// </Summary>
  21. Public ObjectGetclassinstance (StringAssembly,StringNamespace)
  22. {
  23. // The Assembly isProgramSet Name, namespace is the namespace
  24. Clstype = assembly. Load (assembly). GetType (String. Concat (namespace,".",This. Classname ));
  25. If(Clstype =Null)
  26. Return Null;
  27. ObjectOBJ = (Object) Activator. createinstance (clstype );
  28. ReturnOBJ;
  29. }
  30. /// <Summary>
  31. /// You do not need to specify the class name for the Instance Object
  32. /// </Summary>
  33. Public ObjectGetclassinstance (StringAssembly,StringNamespace,StringClassname)
  34. {
  35. Classname = classname;
  36. Clstype = assembly. Load (assembly). GetType (String. Concat (namespace,".", Classname ));
  37. If(Clstype =Null)
  38. Return Null;
  39. ObjectOBJ = (Object) Activator. createinstance (clstype );
  40. ReturnOBJ;
  41. }
  42. /// <Summary>
  43. /// Static method of execution class
  44. /// </Summary>
  45. /// <Param name = "methodname">
  46. /// Method Name of the class
  47. /// </Param>
  48. /// <Param name = "Parameters">
  49. /// Method parameter type
  50. /// </Param>
  51. /// <Param name = "methodtype">
  52. /// Method parameters
  53. /// </Param>
  54. Public ObjectGetmethod (StringMethodname, type [] methodtype,Object[] Parameters)
  55. {
  56. // Methodtype. setvalue (typeof (string), 1 );
  57. System. reflection. methodinfo pmethod = clstype. getmethod (methodname, bindingflags. ignorecase | bindingflags. Static | bindingflags. Public,Null, Methodtype,Null);
  58. // Some flag spaces of the call method. This field indicates public and instance method, which is also the default value.
  59. // System. reflection. bindingflags flag = bindingflags. ignorecase | bindingflags. Static | bindingflags. Public;
  60. ObjectReturnvalue = pmethod. Invoke (Null, Parameters );
  61. // String returnvalue = pmethod. Invoke (clsobj, flag, type. defaultbinder, parameters, null). tostring ();
  62. ReturnReturnvalue;
  63. }
  64. }
  65. }< br>
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.