C # Plug-in program development

Source: Internet
Author: User

What is plug-in programming?
When talking about plug-ins, we first think of firefox. Anyone who has used firefox knows that it is a plug-in program. When a function is required, you can download a plug-in from the Internet and restart it. This feature brings us a lot of convenience, which is the benefit of plug-in programs.

The essence of the plug-in is to expand and strengthen the software functions without modifying the program entity (Platform). When the plug-in interface is published, any company or individual can create their own plug-ins to solve some operational inconvenience or add new features, that is, to truly implement "plug-and-play" software development.

The Platform + plug-in software structure divides a target software to be developed into two parts: the main body or framework of the software, which can be defined as a platform. This is a pre-compiled program. The other part is a function or supplementary module, which can be defined as a plug-in. This is the plug-in program to be installed later.

Assume that your program has been deployed on your computer and runs properly. But one day, the user calls-they need to add new features. After determining the user's requirements, you can find that the original software architecture is no longer qualified for new tasks-you need to redesign this application! But the problem is that even if you use another development cycle to complete the applications required by the user, there is no guarantee that the user's requirements will not change again. That is to say, the possibility of spreading demand still exists. Therefore, the plug-in architecture can better demonstrate its superiority in this case.

It can be said that it can bring convenience, and it is also very easy to develop. In addition, such a main program does not need to be modified at all. You can use the plug-in when you need it. When the plug-in is updated, you only need to update the plug-in.

From the perspective of program development, it is generally to first develop the main program, decide which functions are completed by the main program, and then create an interface to declare the interface content, these contents determine the extension and direction of the plug-in functions. These are pre-prepared by the main program developers. The plug-in developer obtains the interface content from the main program developer, and writes classes that inherit these interfaces to complete specific functions.

The following is an example. This example has no practical significance and is purely a learning idea. The example is self-developed on the Internet, and it is found that some of others are unreasonable.

First, create a new class library that defines the interface. Here two methods are defined, one with a returned value and the other with no returned value.


Using System; using System. collections. generic; using System. text; namespace IMsg {// <summary> // This is the interface that the plug-in must implement and the only interface for communication between the main program and the plug-in. // In other words, the main program only recognizes these methods in the plug-in. // </summary> public interface IMsgPlug {void OnShowDlg (); string OnShowInfo ();}}

Generate IMsg. dll from the above class library, create a class library MYPlugin1, add the just-generated reference, and create two classes respectively 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 Member

Public void OnShowDlg ()

{

Console. WriteLine ("calling the OnShowDlg method of the plug-in on the Console ");

}

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 Member

Public void OnShowDlg ()

{

This. Text = "ins subforms ";

This. ShowDialog ();

// Call ShowDialog of Form to display the Form

}

Public string OnShowInfo ()

{

Return "MyDlg ";

}

# Endregion

}

}

Generate the above dll and set the directory to the bin directory plugins folder of the newly created exe project. The Plugins folder is created to store plug-ins. Create a WinForm project to use the plug-in.

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>

/// Store the plug-in Set

/// </Summary>

Private ArrayList plugins = new ArrayList ();

Public FormMain ()

{

InitializeComponent ();

}

/// <Summary>

/// Load all plug-ins

/// </Summary>

Private void LoadAllPlugs ()

{

// Obtain 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 the dll

Assembly AB = Assembly. LoadFrom (file );

Related Article

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.