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