Use reflection to call methods in any. net Library

Source: Internet
Author: User


The function is as follows. For more information, see :)
Note:
1. ReturnMessage is a self-written class. For more information, see my other articles.
2. You can use NameSpaceAndClassName and MethodName to precisely locate a method, such as calling abc. namespace1.Class1 in dll. main, called CallAssembly ("abc. dll "," namespace1.Class1 "," Main ", null)
 
Public static ReturnMessage CallAssembly (string Path, string NameSpaceAndClassName, string MethodName, object [] Parameters)
{
Try
{
Assembly Ass = Assembly. LoadFrom (Path); // call a file (not limited to dll, exe, as long as it is. net)
Type TP = Ass. GetType (NameSpaceAndClassName); // NameSpaceAndClassName is "namespace. Class Name", for example, "namespace1.Class1"
MethodInfo MI = TP. GetMethod (MethodName); // MethodName is the name of the method to be called, such as "Main"
Object MeObj = System. Activator. CreateInstance (TP );
MI. Invoke (MeObj, Parameters); // Parameters is the list of Parameters passed in when the target method is called.
Return new ReturnMessage (true, "successful call", 1 );
}
Catch (Exception e)

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.