Calling methods in any. NET Library with Reflection

Source: Internet
Author: User
Tags bool reflection
The functions are as follows, there are comments, please view:) Note: 1, ReturnMessage is a class that I wrote, the role please check my other articles, I also attach this class together in the back 2, By Namespaceandclassname and MethodName you can actually pinpoint a method, such as calling the Namespace1.Class1.Main in Abc.dll, which is 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);//Transfer file (not limited to Dll,exe also, as long as. NET)
Type Tp=ass.gettype (namespaceandclassname);//namespaceandclassname is "namespace. Class name", such as "Namespace1." Class1 "
MethodInfo MI=TP. GetMethod (methodname);//methodname is the name of the method to invoke, such as "Main"
Object Meobj=system.activator.createinstance (TP);
MI. Invoke (meobj,parameters);//parameters is the list of arguments passed in when the target method is invoked
return new ReturnMessage (true, "successful invocation", 1);
}
catch (Exception e)
{
return new ReturnMessage (False, "an exception occurred, the message is:" +e.message,-1,e);
}
Attached Returnmessagepublic class ReturnMessage
{
Public ReturnMessage ()
{
This.m_succeed=false;
This.m_message= "";
this.m_code=-1000;
This.m_data=null;
}
Public ReturnMessage (bool issucceed)
{
This.m_succeed=issucceed;
}
Public ReturnMessage (BOOL issucceed,string message)
{
This.m_succeed=issucceed;
This.m_message=message;
}
Public ReturnMessage (bool issucceed,string message,int Code)
{
This.m_succeed=issucceed;
This.m_message=message;
This.m_code=code;
}
Public ReturnMessage (bool issucceed,string message,int code,object Data)
{
This.m_succeed=issucceed;
This.m_message=message;
This.m_code=code;
This.m_data=data;
}
Public ReturnMessage (bool issucceed,string message,int code,object data,object[] Datas)
{
This.m_succeed=issucceed;
This.m_message=message;
This.m_code=code;
This.m_data=data;
This.m_datas=datas;
}
//
BOOL M_succeed;
String M_message;
int M_code;
Object m_data;
Object[] M_datas;
public bool Succeed
{
Get{return M_succeed;}
Set{m_succeed=value;}
}
public string Message
{
Get{return M_message;}
Set{m_message=value;}
}
public int Code
{
Get{return M_code;}
Set{m_code=value;}
}
public Object Data
{
Get{return m_data;}
Set{m_data=value;}
}
Public object[] Datas
{
Get{return M_datas;}
Set{m_datas=value;}
}
}


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.