The C # language can develop many program plug-ins, including browser plugins and so on.
By the the-the-word, speaking of plugins,
The first thing we think about is the browser, Firefox, Firefox has been used to know that it is a plug-in program. When a feature is needed, a plugin can be downloaded completely from the web, and after a reboot, it can be used. This feature brings us a lot of convenience, which is the benefits of plug-in programs.
The essence of plug-ins is not to modify the program body (platform) to expand and strengthen the software functions, when the interface of the plug-in is open, any company or individual can make their own plug-ins to solve some operational inconvenience or add new features, that is, the real sense of "plug and Play" software development.
Platform + plug-in software structure is a development of the target software into two parts, part of the software's main body or framework, can be defined as a platform, which is a pre-compiled program. The other part is a function or supplemental module, which can be defined as a plug-in. This is the plug-in program that will be installed later.
Assume that your program is already deployed on the user's computer and that it is functioning properly. But one day, users call-they need to add new features. Once you've identified the user's needs, you've found that the original software architecture is not up to the requirements of the new task-you need to redesign the app! But the problem is, even if you use a development cycle to complete the user needs of the application, cut does not guarantee that the user's needs will not be changed again. In other words, the likelihood of a demand spread is still there. Therefore, the plug-in architecture can show its superiority in this case.
So to speak, it can be used to bring convenience, and to develop it, it is also very simple. and such a master program does not need to change at all. when you need a plug-in, you can use it, plug-in updates, you just need to update the plugin.
From the perspective of the development of the program, it is generally the first development of the main program, decide which functions to be completed by the main program, and then establish the interface, affirm the content of the interface, these content determines the plug-in function extension and direction. These are pre-prepared by the main program developer. Plug-in developers, from the main program developers to get the content of the interface, and write the classes that inherit these interfaces, to complete the specific functions.
Here is an example, this example is not meaningful, purely learning ideas. Example is the online through their own transformation, found that others in some places unreasonable.
First, create a new class library that defines the interface, which defines two methods, one with a return value, and one with no return value.
using System;
using System.Collections.Generic;
using System.Text;
namespace imsg
{
/// <summary>
/// This is the interface that the plugin must implement, and the only interface that the main program communicates with the plug-in.
/// In other words, the main program only knows these methods in the plugin
/// </summary>
Public Interface Imsgplug
{
void Onshowdlg ();
string Onshowinfo ();
}
}
Generate the above class library IMsg.dll, create a new class library MYPlugin1, add the just-out reference, create a new two classes to implement the interface defined in imsg.
using System;
using System.Collections.Generic;
using System.Text;
using imsg;
namespace MYPlugin1
{
Public class Myconsole:imsgplug
{
#region Imsgplug Members
Public void Onshowdlg ()
{
Console.WriteLine ( " Onshowdlg method for the console call plug-in " );
}
Public string Onshowinfo ()
{
return " MyConsole " ;
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using imsg;
namespace MYPlugin1
{
Public class Mydlg:form,imsgplug
{
#region Imsgplug Members
Public void Onshowdlg ()
{
This . Text = " plug-in subform " ;
This . ShowDialog (); // calls the form's ShowDialog, displays the forms
}
Public string Onshowinfo ()
{
return " Mydlg " ;
}
#endregion
}
}
To build the DLL above, the build directory can be set to the new EXE project under the Bin Directory Plugins folder. The plugins folder is a new, specially stored plugin. Create a new WinForm project to use the plugin just now. Figure:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Reflection;
namespace Msgboxmain
{
Public Partial class Formmain:form
{
/// <summary>
/// storing a collection of plugins
/// </summary>
Private ArrayList Plugins = New ArrayList ();
Public FormMain ()
{
InitializeComponent ();
}
/// <summary>
/// Load All Plugins
/// </summary>
Private void loadallplugs ()
{
// get all files under the plug-in directory (plugins)
string [] Files = Directory.GetFiles (Application.startuppath + @" \plugsins " );
foreach ( string file inch Files
{
if (file. ToUpper (). EndsWith ( " . DLL " ))
{
Try
{
// Load DLL
Assembly AB = assembly.loadfrom (file);
Type[] Types = Ab. GetTypes ();
foreach (Type t inch types)
{
// If some classes implement a predefined Imsg.imsgplug interface, the class is considered to be adaptable to the main program (the plugin for the main program)
if (T.getinterface ( " Imsgplug " ) != NULL )
{
Plugins. ADD (AB. CreateInstance (T.fullname));
LISTBOX1.ITEMS.ADD (T.fullname);
}
}
}
Catch (Exception ex)
{
MessageBox.Show (ex. Message);
}
}
}
}
Private void Btnloadplug_click ( Object sender, EventArgs e)
{
Loadallplugs ();
}
// methods for invoking plug-ins
Private void Btnexecute_click ( Object sender, EventArgs e)
{
if ( This . Listbox1.selectedindex == - 1 ) return ;
Object Selobj = This . plugins[ This . Listbox1.selectedindex];
Type T = Selobj.gettype ();
MethodInfo Onshowdlg = T.getmethod ( " Onshowdlg " );
MethodInfo Onshowinfo = T.getmethod ( " Onshowinfo " );
Onshowdlg.invoke (Selobj, NULL );
Object returnvalue = Onshowinfo.invoke (Selobj, NULL );
This . Lblmsg.text = returnvalue.tostring ();
}
}
}
Operation Result:
The following is a discussion of plug-in code:
if (Itemstr = = "MyConsole")
{//Call the Onshowinfo method of the plug-in object stored in the dynamic array plugins
String msginfo = ((Imsgplug) Plugins[listitems.selectedindex]). Onshowinfo ();
MessageBox.Show (Msginfo, "MYPlugin1", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
ElseIf (Itemstr = = "Mydlg")//Call the Onshowdlg method of the plug-in object stored in the dynamic array plugins
{
((Imsgplug) Plugins[listitems.selectedindex]). Onshowdlg ();
}
I think that since it is a plug-in, it should be dynamically loaded, the client must not Judge Itemstr, because the implementation of the interface class is unpredictable, so the main program should not add a reference to imsg, and should not be instantiated in the client plug-in object, Because plug-in development is intended to be updated at a later time do not change the main program, only provide the corresponding DLL download, you can directly use, the previous interface is defined, the new implementation class is unpredictable, and therefore can not be instantiated in the main program to implement the interface class, which violates the original intention of the plug-in.
End
Analysis of C # plug-in program