|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionI guess I need to start off with the customary, "This is my first CodeProject article, so please be nice." Now that it's been said, let's move on the the meat of this article. The sample project/application was designed to demonstrate the creation of an infrastructure within an application in order to provide some extensibility through plug-ins. This project is hosted on SourceForge. If you wish to contribute to it, provide feedback or alternative approaches, please feel free to contact me. The solution provided, Plug-in, includes five C# projects:
Technologies used
Quick and dirty: how-toCreate the plug-in hostStep 1: Include a reference to the TaskPluginInterface.dll in your solution. Step 2: Get the folder location for the plug-ins. I use \Plugins. Call the Step 3: Get a list of plug-ins. Call Step 4: Call the plug-ins Execute method. Create the plug-inOption 1: code from scratchStep 1: Include a reference to the TaskPluginInterface.dll in your solution. Step 2: Create a class that inherits from ITaskPlugin. public class RunCommand : ITaskPlugin
{
}
Step 3: If you are using Visual Studio 2005, you can use the Intellisense or SmartTag to create the stub implementation of ITaskPlugin. Step 4: Place your code in the Execute method. Option 2: use the PluginShellPlugin classStep 1: Rename the class to what ever name you want. Step 2: Change the resource file strings to accurately describe your plug-in. Step 3: Change the code in the TaskPluginInterface namespaceITaskPlugin interfaceThe ITaskPlugin interface defines the methods, events and properties that the plug-ins must implement. Methods
Properties
Events
PluginAttribute classThe PluginAttribute class defines the class attributes which can be applied to a class that is implementing the ITaskPlugin interface. This class defines an Attribute, which is PluginType. The PluginType corresponds to the PluginType class. Syntax: TaskPluginInterface.Plugin(PluginType.Executable)]
public class RunCommand : ITaskPlugin
{
}
EnumerationsExecuteResultThis enumeration is used as the return value of the Execute method for the ITaskPlugin interface.
PluginTypeThis enumeration is used to indicate the type of plug-in that is being created. Applying the PluginType attribute to a class does not have an effect the functionality of the class. Its only purpose is to provide a classification of the plug-ins available for list boxes or separate functionality.
PluginEventArgs classThe PluginEventArgs class provides information back to the plug-in host. This information is provided back to the plug-in host via one of the four events. PluginEventArgs properties:
ScheduleConfig classThe ScheduleConfig class provides functions that will load the schedule configuration. Note that this was created for a future version of the application. Utilities classThe Utilities class provides helper functions to interact with and discover plug-ins. All of the provided methods are static, so there is no need to create an instance of the Utilities class. Utilities class methods:
Included plug-insFileCopyThe FileCopy plug-in provides the ability to copy files that fit a mask from one directory to another. This plug-in reads the configuration file, FileCopyPlugin.Settings.xml, for all of the Directory nodes listed in the Directories element. For example: <Directories>
<Directory FromDir="C:\Temp\" ToDir="d:\Temp\" FileMask="*.*" />
<Directory FromDir="C:\Temp\junk" ToDir="d:\Temp\junk" FileMask="*.txt" />
<Directory FromDir="C:\Temp\junk" ToDir="d:\Temp\junk" FileMask="*.xyz" />
</Directories>
The following configuration contains three copy commands.
PluginShellThe PluginShell plug-in provides a class that contains everything needed to implement the ITaskPluginInterface. RunCommandThe RunCommand plug-in provides the ability to execute processes using the System.Diagnostics.Process namespace. This plug-in reads the configuration file. RunCommandPlugin.Settings.xml, for Command elements listed in the Commands node. Each command element has XmlChildNodes that correspond to parameters of the ProcessStartInfo struct of the Process object. TaskPluginTestThe TaskPluginTest is a sample application that demonstrates the use of the TaskPluginInterface library. This application utilizes the TaskPluginInterface utilities to load the list of application plug-ins. In addition, this application displays the plug-in metadata, executes the plug-in and handles events from the plug-in. Future enhancements
References
History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||