Very good C # dynamic calling of DLL Code

Source: Internet
Author: User

1. Create a call project and a class project to be tested. First, design the test class and function, and compile and generate the corresponding class name. dll file. Copy it to the bin directory of the calling project.

 

2. Create a winform in the call Project, pull a button in the toolbox to the form, and write the call code in the button event.

// Call Method
Private void button#click (Object sender, eventargs E)
{

// Call Method
Dllmanager mydllmanager = new dllmanager ();
// Output result
Console. Write (mydllmanager. Invoke ("Your DLL file name. dll", "namespace name", "Class Name", "function name", null ));
// Uninstall the DLL
Mydllmanager. unloaddll ();
}

III. The DLL class file for dynamic calling is as follows:

// Add reference
Using system. IO;
Using system. reflection;

Public class dllmanager
{

Private Static Assembly myassembly; // record the assembly to be imported

/// <Summary>
/// Call the specified function of the specified class in the specified DLL
/// </Summary>
/// <Param name = "lpfilename"> DLL file name or path </param>
/// <Param name = "namespace"> namespace in DLL </param>
/// <Param name = "classname"> class in the namespace </param>
/// <Param name = "lpprocname"> method in the class </param>
/// <Param name = "objarray_parameter"> method parameters </param>
/// <Returns> </returns>
Public object invoke (string lpfilename, string namespace, string classname, string lpprocname, object [] objarray_parameter)
{

Try
{// Determine whether myassembly is null or whether the namespace of myassembly is not equal to the namespace of the method to be called. If the condition is true, use assembly. Load to load the required DLL as the Assembly

If (myassembly = NULL | myassembly. getname (). Name! = Namespace)

Myassembly = assembly. Load (loaddll (lpfilename ));

Type [] type = myassembly. gettypes ();

Foreach (type T in type)
{

If (T. namespace = namespace & T. Name = classname)
{

Methodinfo M = T. getmethod (lpprocname );

If (M! = NULL)
{// Call and return

Object o = activator. createinstance (t );

Return M. Invoke (O, objarray_parameter );

}

Else

System. Windows. Forms. MessageBox. Show ("loading error! ");

}

}

}

Catch (system. nullreferenceexception E)
{

System. Windows. Forms. MessageBox. Show (E. Message );

}

Return (object) 0;

}
/// <Summary>
/// Load the DLL file
/// </Summary>
/// <Param name = "lpfilename"> DLL file name or path </param>
/// <Returns> </returns>
Private byte [] loaddll (string lpfilename)
{

Assembly nowassembly = assembly. getentryassembly ();

Stream FS = NULL;

Try
{// Try to read the DLL from the Resource

FS = nowassembly. getmanifestresourcestream (nowassembly. getname (). Name + "." + lpfilename );

}

Finally
{// If the resource does not have the required DLL, check whether there is any DLL on the hard disk. If yes, read it.

If (FS = NULL &&! File. exists (lpfilename) Throw (new exception ("file not found:" + lpfilename ));

Else if (FS = NULL & file. exists (lpfilename ))
{

Filestream FS = new filestream (lpfilename, filemode. Open );

FS = (Stream) FS;

}

}

Byte [] buffer = new byte [(INT) fs. Length];

FS. Read (buffer, 0, buffer. Length );

FS. Close ();

Return buffer; // return the read DLL with byte []

}

Public void unloaddll ()
{
// Empty myassembly
Myassembly = NULL;

}
}

// The above code is collected by the website. You have tested your own database operation DLL files. No problem. I hope it will be convenient for you.

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.