C # simple use of reflection in development

Source: Internet
Author: User

In the past, I wrote an example of a good DLL image in Windows Mobile. Now, in the project, there is a need to dynamically load some forms in a large form. Develop functions into separate DLL Based on the module division. The main framework dynamically loads different DLL types to meet different requirements.
1. The following is a simple example. There is a button click event. Click the event to load the form in the known DLL.

Private void button#click (Object sender, eventargs e) {// click to load the form form4 string dllname = "classlibrary1.dll"; string formname = "classlibrary1.form4"; form FF = NULL; // load DLL Assembly myassembly = assembly. loadfrom (dllname); // obtain all classes and member types in the DLL [] types = myassembly. gettypes (); Type type = NULL; // traverse the expected Member (form) foreach (type T in types) {If (T. fullname = formname) {type = T; break ;}// instantiate the form FF = (form) activator. createinstance (type); FF. show ();}

-------------------- Mean split line ------------------------------
2. The reflection mechanism calls the methods in DLL.

Public String add (int x, int y) {return X + Y + "";} private void button2_click (Object sender, eventargs E) {// load the DLL information string dllname = "classlibrary1.dll"; string dllnamespace = "classlibrary1"; string classname = "class1"; string methodname = "add "; // call the method parameter object [] parameters = new object [2] {1, 2}; string message = ""; // load the DLL Information Assembly myassembly = assembly. loadfrom (dllname); Type [] types = myassem Bly. gettypes (); // The foreach (type T in types) {If (T. namespace = dllnamespace & T. name = classname) {methodinfo M = T. getmethod (methodname); If (M! = NULL) {// call the method object o = Activator In the DLL. createinstance (t); message = m. invoke (O, parameters ). tostring (); MessageBox. show (Message);} elsemessagebox. show ("loading error! ");}}}

-------------------- Mean split line ------------------------------
3. Use a simple application to dynamically load the forms under the project (seemingly useless)

Private void form1_load (Object sender, eventargs e) {// load the form type = type in this project. getType ("tabwindow. form2 "); form FF = (form) activator. createinstance (type); FF. show ();}

 

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.