Plug-in modules allow extending editor functionality dynamically. Module is a class, its assembly is loaded by editor at startup and class is instantiated. Typical use for plug-ins is adding custom commands to the editor.
Creating plug-in
Installer has sample plugin in the C:\Program Files\Aspose\Aspose.Editor\Demos\CSharp\Plugins\ directory. Let's take a look at the class.
/// <summary>
/// This class has custom attribute which specifies that class is plugin for
/// the editor control.
/// </summary>
[PluginClass("Demo Plugin", "Sample demo plugin which shows how plugins can be created.", "GetCommands")]
public class DemoPlugin
{
/// <summary>
/// Initializes new instance of this class. This constructor has
/// default signature.
/// It will be used by the editor if plugin class wasn't referenced
/// in the configuration or if referenced was constructor without parameters.
/// </summary>
/// <param name="e">Editor.</param>
public DemoPlugin(EditorControl e)
{
// TODO: your code here
}
}
This class has attribute PluginClass which tells editor that class is a plug-in and must be instantiated. Class must be declared public and must have public constructor which takes editor as an argument. Class can have other constructors but only if constructor is public and has editor as a first parameter it can be configured to be called by the editor.
Inside the constructor commands can be created and added to the editor as well as any other customization directives executed.
Note: plug-in needs to reference Aspose.Editor.Desktop or Aspose.Editor.Client assembly. This means that it can be used with this specific versions of an assemblies used during build.
Configuring editor
In order to notify editor about plug-ins it needs to load we must add corresponding entry into the application configuration file. For the desktop application Aspose.Editor.Demo.exe configuration file is Aspose.Editor.Demo.exe.config and located in the same directory where application is. For ASP.NET application special tag must be embedded into the page to refer to the configuration file. Here is an example from the Aspose.Editor.Demo.Web included into the installer.
<link rel="configuration" href="ClientBin/Aspose.Editor.Client.dll.config">
Content of the file can have many settings and we'll need to add entry in the corresponding section. If there is no configuration file yet it must be created.
<configuration>
<appSettings>
<!-- This setting specifies full assembly name of the assembly which contains plug-in
modules for the editor control. Specify 'add' tag for each available assembly.
Each key must be unique but shall start with "Aspose.Editor.Plugin". -->
<add key="Aspose.Editor.Plugin.Sample" value="Aspose.Editor.Plugins.DemoPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</appSettings>
</configuration>
Here we specify application setting with key Aspose.Editor.Plugin.Sample, value specifies assembly name. CLR will use standard probing algorithm to find the assembly. Assembly if found will be loaded into the default load context. Typically plug-in assemblies are located in the same directory where application assembly and configuration file are.
There is no need to rebuild application; each time instance of the editor is created it will load plug-ins automatically. Each working instance of the editor creates its own plugin instances.
Plug-in entry can specify either assembly alone, or assembly and class name, or assembly, class name and specific constructor. Here are examples:
<configuration>
<appSettings>
<add key="Aspose.Editor.Plugin.1" value="MyPlugins"/>
<add key="Aspose.Editor.Plugin.2" value="MyPlugins.ThePlugin@MyPlugins"/>
<add key="Aspose.Editor.Plugin.3" value="MyPlugins.CustomPlugin
(true, "key", new object[] { Color.Black } )@MyPlugins, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null"/>
</appSettings>
</configuration>
Note: If you want to load multiple plug-in assemblies, please add one tag per assembly (or plugin class). Make sure key is unique but starts with "Aspose.Editor.Plugin".
If plugin entry refers to the assembly name only then loading order of the plug-ins in that assembly is not defined, however plug-in assemblies are loaded in the same order they appear in the configuration file.
Note that because plug-in modules are loaded into the default application domain they cannot be unloaded until application is restarted.