Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Extension Points and Extensions

slluis edited this page Nov 20, 2014 · 4 revisions
Home > Programming Guide > Extension Points and Extensions

A typical Mono.Addins based application is composed of:

  • A host application (or add-in host), which defines several extension points.
  • Several add-ins, which extend extension points defined by a host.

Implementing an add-in host

The following code is a basic example of an add-in host application. This application runs a set of commands which can be implemented by add-ins:

using System;
using Mono.Addins;

[assembly:AddinRoot ("HelloWorld", "1.0")]

class MainClass
{
	public static void Main ()
	{
		AddinManager.Initialize ();
		AddinManager.Registry.Update ();
		
		foreach (ICommand cmd in AddinManager.GetExtensionObjects<ICommand> ())
			cmd.Run ();
	}
}

This code shows some declarations and initializations that you have to do in all extensible applications:

  • The [AddinRoot] attribute applied to an assembly declares that this assembly is an add-in host. All add-in hosts must have an identifier and a version number.
  • The AddinManager.Initialize call initializes the add-in engine. It must be called before doing any operation with the AddinManager class.
  • When calling AddinManager.Registry.Update, the add-in engine will scan the application directory (and other user-defined add-in directories) looking for add-ins, and will update an internal add-in registry which is used to cache add-in information.
  • The AddinManager.GetExtensionObjects call can be used to query an extension point. It returns a list of objects registered in that extension point.

However, for this example to be complete, the extension point must be declared.

Declaring the Extension Point

The extension point for the above example can be declared like this:

using Mono.Addins;

[TypeExtensionPoint]
public interface ICommand
{
	void Run ();
}

The [TypeExtensionPoint] attribute when applied to a class or interface declares an extension point which accepts objects of that type. In this example, we are creating an extension point for objects implementing the type ICommand.

Implementing the add-in

An add-in extending the above extension point would look like this:

using System;
using Mono.Addins;

[assembly:Addin]
[assembly:AddinDependency ("HelloWorld", "1.0")]

[Extension]
public class HelloCommand: ICommand
{
	public void Run ()
	{
		Console.WriteLine ("Hello World!");
	}
}

Add-ins are implemented in separate assemblies, which must be marked with the [Addin] attribute to be recognized as add-ins. Add-ins must also declare the add-in hosts they are extending by using the [AddinDependency] attribute. Notice that an add-in can extend several add-in hosts, and it can even extend other add-ins which declare their own extension points. The AddinDependency attribute must specify the host/add-in id and its version number.

By applying the [Extension] attribute to a class we are declaring a new extension node of that type which is added to an extension point. The extension point is determined by looking at the base types and interfaces of the class. So in this example the class implements the ICommand interface, and there is an extension point defined for that interface, so that's the extension point where the type will be registered. If a class implements several interfaces, we can select the extension point by specifying the type name in the attribute, for example: [Extension (typeof(ICommand))].

More Information

The Description of Add-ins and Add-in Roots section in the reference manual has more information about creating extensions and extension points, including:

Next section: Querying Extension Points

Clone this wiki locally