Mono.Addins 0.4.0.0 1.0.0.0 System.Object An extension context.

Extension contexts can be used to query the extension tree using particular condition values.

Extension points which declare the availability of a condition type can only be queryed using an extension context which provides an evaluator for that condition.

This is a complete example of using ExtensionContext together with conditions. In this example, we want to implement an extensible library called "WriterService" which offers the following class:

`namespace WriterService { public class WriterManager { public WriterManager (string[] flags) { ... } public IWriter[] GetWriters () { ... } } }`

The IWriter interface is defined like this:

`namespace WriterService { public interface IWriter { string Write (); } }`

An application can use this library to get a set of writers, which will be implemented in add-ins extending WriterService. We also want add-ins to be able to decide which writers should be returned, depending on some flags defined provided by the user. Notice that different WriterManager instances can have different flags.

This is an example of consumer application:

`Console.WriteLine ("Normal writers:"); // Creates a WriterManager which will return all writers WriterManager manager = new WriterManager (new string[0]); // Get the writers and print them foreach (IWriter w in manager.GetWriters ()) Console.WriteLine (w.Write ()); Console.WriteLine ("Including debug writers:"); // Now this creates a new WriterManager, and providers a debug flag WriterManager debugManager = new WriterManager (new string[] { "debug" }); foreach (IWriter w in debugManager.GetWriters ()) Console.WriteLine (w.Write ());`

THE WRITER-SERVICE LIBRARY

The WriterService library needs to define an extension point where add-ins can register new writer types. The add-in description file could be the following:

`<Addin id = "WriterService" version = "0.1.0" isroot="true"> <Runtime> <Import assembly="WriterService.dll"/> </Runtime> <ExtensionPoint path = "/WriterService/Writers"> <ExtensionNode id="Writer" objectType="WriterService.IWriter"/> <Condition id="HasFlag"/> </ExtensionPoint> </Addin>`

Notice that the Writers extension point specifies that a "HasFlag" condition type which can be used by extenders when defining extension nodes. This condition will be implemented by the WriterService in the FlagsCondition class:

`namespace WriterService { public class FlagsCondition: ConditionType { string[] flags; public FlagsCondition (string[] flags) { this.flags = flags; } public override bool Evaluate (INodeInfo attributes) { // Return true if the provided flag can be found in the condition's flag list string flag = attributes.GetAttribute ("value"); return Array.IndexOf (flags, flag) != -1; } } }`

Now let's see the complete implementation of the WriterManager class:

`namespace WriterService { public class WriterManager { ExtensionContext ctx; public WriterManager (string[] flags) { // Create a new extension context ctx = AddinManager.CreateExtensionContext (); // Register the flags condition in the new context FlagsCondition condition = new FlagsCondition (flags); ctx.RegisterCondition ("HasFlag", condition); } public IWriter[] GetWriters () { // Returns the IWriter objects registered in the Writers path return (IWriter[]) ctx.GetExtensionObjects ("/WriterService/Writers", typeof(IWriter)); } } }`

The WriterManager class creates a new ExtensionContext instance and registers a properly initialized FlagsCondition object on it. The ExtensionContext instance is needed because every WriterManager needs to have it's particular FlagsCondition object. The GetWriters method calls on the created extension context to get the extension nodes, so it will only return those matching the registered condition.

In this case it would not be possible to use the global method to get the writers because the HasFlag condition is not registered at global level, only for a particular context.

SAMPLE ADD-IN

This is a sample add-in which extends the WriterService by adding two writer types. One of them is under a condition, so it will only be visible if the debug flag is defined:

`<Addin> <Runtime> <Import assembly="SampleExtender.dll"/> </Runtime> <Dependencies> <Addin id="WriterService" version="0.1.0" /> </Dependencies> <Extension path = "/WriterService/Writers"> <Writer type="SampleExtender.MyWriter" /> <Condition id="HasFlag" value="debug"> <Writer type="SampleExtender.DebugWriter" /> </Condition> </Extension> </Addin>`

The new writers are implemented like this:

`namespace SampleExtender { public class MyWriter: IWriter { public string Write () { return "Some writer"; } } public class DebugWriter: IWriter { public string Write () { return "Some debug output"; } } }`

The sample application shown earlier would give the following output after installing the previous add-in:

`Normal writers: Some writer Including debug writers: Some writer Some debug output`
- Method
  System.Void `` `` Path of the node. A handler method. Register a listener of extension node changes. This method registers a delegate to be called when a node is added to or removed from the provided extension path.

  The handler will be called for all nodes existing in the path at the moment of registration.

  0.4.0.0
- Event
  Mono.Addins.ExtensionEventHandler To be added. To be added. 0.4.0.0
- Method
  0.4.0.0
  System.Void To be added. To be added.
- Method
  Mono.Addins.ExtensionNode `` Location of the node. Returns the extension node in a path The node, or null if not found.

  This method returns the extension node located at the provided path. For example, given the following extension point:

  `<Extension path="/MonoDevelop/Workbench/MainMenu"> <ItemSet id="File" _label="_File"> <CommandItem id="NewFile" /> <CommandItem id="NewProject" /> </ItemSet> </Extension>`

  The following calls are valid:

  `ExtensionContext ctx; ... // The following call returns the root element of the extension point ExtensionNode node = ctx.GetExtensionNode ("/MonoDevelop/Workbench/MainMenu"); // The following call returns an specific node of the extension point ExtensionNode node = ctx.GetExtensionNode ("/MonoDevelop/Workbench/MainMenu/File"); // The following call returns a child of a node of the extension point ExtensionNode node = ctx.GetExtensionNode ("/MonoDevelop/Workbench/MainMenu/File/NewProject");` 0.4.0.0
- Method
  Mono.Addins.ExtensionNodeList `` An extension path. Gets extension nodes registered in a path. All nodes registered in the provided path.

  This method returns all nodes registered under the provided path. For example, given the following extension point:

  `<Extension path="/MonoDevelop/Workbench/MainMenu"> <ItemSet id="File" _label="_File"> <CommandItem id="NewFile" /> <CommandItem id="NewProject" /> </ItemSet> <ItemSet id="Edit" _label="_Edit"/> <ItemSet id="View" _label="_View"/> </Extension>`

  The following calls are valid:

  `ExtensionContext ctx; ... // The following call prints 'File', 'Edit' and 'View' ExtensionNodeList nodes = ctx.GetExtensionNodes ("/MonoDevelop/Workbench/MainMenu"); foreach (ExtensionNode node in nodes) Console.WriteLine (node.Id); // The following call prints 'NewFile' and 'NewProject' nodes = ctx.GetExtensionNodes ("/MonoDevelop/Workbench/MainMenu/File"); foreach (ExtensionNode node in nodes) Console.WriteLine (node.Id);` 0.4.0.0
- Method
  Mono.Addins.ExtensionNodeList `` `` An extension path. Expected node type. Gets extension nodes registered in a path. All nodes registered in the provided path. This method returns all nodes registered under the provided path. It will throw a if the type of one of the registered nodes is not assignable to the provided type. 0.4.0.0
- Method
  System.Object[] `` An extension path. Gets extension objects registered in a path. An array of objects registered in the path. This method can only be used if all nodes in the provided extension path are of type . The returned objects are created by calling . 0.4.0.0
- Method
  System.Object[] `` Type for which to query extension objects. Gets extension objects registered in an extension point. An array of objects registered in the extension point.

  This method can be used to query the extension point of a type which does not have a path. For example:

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

  The previous interface declares an extension point which will accept types that implement ICommand interface. Since the extension path is not provided in this declaration, the host has to use the GetExtensionObject method to get the registered object. For example:

  `ExtensionContext context; ... foreach (ICommand cmd in context.GetExtensionObjects (typeof(ICommand))) cmd.Run ();`

  The returned array is composed by all objects created by calling the method for each node.

  0.4.0.0
- Method
  System.Object[] `` `` An extension path. True if the method can reuse instances created in previous calls. Gets extension objects registered in a path. An array of objects registered in the path. This method can only be used if all nodes in the provided extension path are of type . The returned array is composed by all objects created by calling the method for each node (or if reuseCachedInstance is set to true). 0.4.0.0
- Method
  System.Object[] `` `` An extension path. Type of the return array elements. Gets extension objects registered in a path. An array of objects registered in the path. This method can only be used if all nodes in the provided extension path are of type . The returned objects are created by calling .

  A exception is thrown if one of the found objects is not a subclass of the provided type.

  0.4.0.0
- Method
  System.Object[] `` `` Type for which to query extension objects. True if the method can reuse instances created in previous calls. Gets extension objects registered in an extension point. An array of objects registered in the extension point.

  This method can be used to query the extension point of a type which does not have a path. For example:

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

  The previous interface declares an extension point which will accept types that implement ICommand interface. Since the extension path is not provided in this declaration, the host has to use the GetExtensionObject method to get the registered object. For example:

  `ExtensionContext context; ... foreach (ICommand cmd in context.GetExtensionObjects (typeof(ICommand))) cmd.Run ();`

  The returned array is composed by all objects created by calling the method for each node (or if reuseCachedInstance is set to true).

  0.4.0.0
- Method
  System.Object[] `` `` `` An extension path. Type of the return array elements. True if the method can reuse instances created in previous calls. Gets extension objects registered in a path. An array of objects registered in the path. This method can only be used if all nodes in the provided extension path are of type . The returned array is composed by all objects created by calling the method for each node (or if reuseCachedInstance is set to true).

  A exception is thrown if one of the found objects is not a subclass of the provided type.

  0.4.0.0
- Method
  System.Void `` `` Identifier of the condition. Condition evaluator. Registers a new condition in the extension context. The registered condition will be particular to this extension context. Any event that might be fired as a result of changes in the condition will only be fired in this context. 0.4.0.0
- Method
  System.Void `` `` Identifier of the condition. Type of the condition evaluator. Must be a subclass of Registers a new condition in the extension context.

  The registered condition will be particular to this extension context. Any event that might be fired as a result of changes in the condition will only be fired in this context.

  An instance of the condition will be created the first time it is requested, and it will be cached. 0.4.0.0
- Method
  System.Void `` `` Path of the node. A handler method. Unregister a listener of extension node changes. This method unregisters a delegate from the node change event of a path. 0.4.0.0