Click here to Skip to main content
15,893,508 members
Articles / Programming Languages / C#

Add run-time functionality to your application by providing a plug-in mechanism

Rate me:
Please Sign up or sign in to vote.
4.76/5 (43 votes)
26 May 20038 min read 163.9K   163  
Use Activator and IConfigurationSectionHandler in perfect harmony to add plugin abilities to your application
using System;
using System.Text.RegularExpressions;
using System.Text;
using Royo.Plugins;

namespace Royo.Plugins.Custom
{
	/// <summary>
	/// This class implements the IPlugin interface
	/// to provide the ability to parse and isolate email addresses found within
	/// the current editor's text
	/// </summary>
	public class EmailPlugin:IPlugin
	{
		public EmailPlugin()
		{
		}
		#region IPlugin Members


		/// <summary>
		/// The single point of entry to our plugin
		/// Acepts an IPluginContext object which holds the current
		/// context of the running editor.
		/// It then parses the text found inside the editor
		/// and changes it to reflect any email addresses that are found.
		/// </summary>
		/// <param name="context"></param>
		public void PerformAction(IPluginContext context)
		{
			context.CurrentDocumentText=ParseEmails(context.CurrentDocumentText);
		}

		/// <summary>
		/// The name of the plugins as it will appear 
		/// under the editor's "Plugins" menu
		/// </summary>
		public string Name
		{
			get
			{
				return "Email Parsing Plugin";
			}
		}

		#endregion

		/// <summary>
		/// Parse the given string for any emails using the Regex Class
		/// and return a string containing only email addresses
		/// </summary>
		private string ParseEmails(string text)
		{
			const string emailPattern=@"\w+@\w+\.\w+((\.\w+)*)?";

			MatchCollection emails = Regex.Matches(text,emailPattern,RegexOptions.IgnoreCase);
			StringBuilder emailString = new StringBuilder();
			foreach(Match email in emails)					
			{
				emailString.Append(email.Value + Environment.NewLine);
			}

			return emailString.ToString();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions