C # plug-in development

Source: Internet
Author: User

On the Internet to find the next plug-in programming information, the first to learn from others, but also found their own views, but because I have limited, not necessarily reference value, write to summarize themselves, in order to improve, on the other hand, I also hope that the friends see my shortcomings, give me valuable advice.

What is plug-in programming

Mention plug-in, we first think of is Firefox, used Firefox people know 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 are completed by the main program, and then set up an interface to affirm the content of the interface, these content determines the extension of the plug-in function, 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 of the console call plugin");
}

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 ();//Call Form ShowDialog, display 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.

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 in files)
{
if (file. ToUpper (). EndsWith (". DLL "))
{
Try
{
Load DLL
Assembly ab = assembly.loadfrom (file);
type[] types = ab. GetTypes ();
foreach (Type t in 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:

This is different from the original man on the Internet (the original link http://blog.csdn.net/jam12315/archive/2008/08/18/2791534.aspx), for everyone to discuss.

The original text has such a paragraph:

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

C # plug-in development

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.