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;
}
}