C # How to dynamically call the delegate DLL

Source: Internet
Author: User
Public partial class FrmMain: Form
{
Public FrmMain ()
{
InitializeComponent ();
}

Private void FrmMain_Load (object sender, EventArgs e)
{
Object [] obj = new object [1];
Obj [0] = "DllInvoke ";
Object o2 = DllInvoke ("E: \ Test \ TestDll \ bin \ Debug \ TestDll. dll "," TestDll "," TestDll "," HandleStr ", obj );
MessageBox. Show (o2.ToString ());
}

/// <Summary>
/// Call DLL dynamically
/// </Summary>
/// <Param name = "DllFileName"> dll name </param>
/// <Param name = "NameSpace"> NameSpace </param>
/// <Param name = "ClassName"> class name </param>
/// <Param name = "MethodName"> method name </param>
/// <Param name = "ObjArrayParams"> parameter array </param>
/// <Returns> </returns>
Private object DllInvoke (string DllFileName, string NameSpace, string ClassName, string MethodName, object [] ObjArrayParams)
{
Try
{
// Load the Assembly
Assembly DllAssembly = Assembly. LoadFrom (DllFileName );
Type [] DllTypes = DllAssembly. GetTypes ();
Foreach (Type DllType in DllTypes)
{
// Find the namespace and class to call
If (DllType. Namespace = NameSpace & DllType. Name = ClassName)
{
// Locate and call the method to be called
MethodInfo MyMethod = DllType. GetMethod (MethodName );
If (MyMethod! = Null)
{
Object mObject = Activator. CreateInstance (DllType );
Return MyMethod. Invoke (mObject, ObjArrayParams );
}
}
}
}
Catch (Exception mEx)
{
Throw new Exception (mEx. Message );
}
Return (object) 0;
}
}

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.