Exposing Host Objects to Add-Ins

You must create a host item provider when you integrate Visual Studio Tools for Applications with an application. The host item provider enables add-ins to access the object model of the application. For more information about modifying an application so that it loads and unloads add-ins, see Discovering and Loading Add-Ins. For code examples that demonstrate how to create host item providers, see Walkthrough: Modifying an Application to Load Add-Ins.

Understanding Host Item Providers

A host item provider is a class that exposes host objects to add-ins. This class implements the IHostItemProvider interface in the host application. This interface defines a single method, GetHostObject. This method returns an instance of a type in the object model of the host application that corresponds to an entry point in the add-in. For more information about entry points, see Defining Entry Points and Other Proxy Changes.

When the host application loads an add-in, each entry point in the add-in calls the GetHostObject method to obtain an instance of its corresponding type in the host application. Each time the add-in calls a member of the entry point, the member uses this object to call into the corresponding member of the object in the host application. For more information, see Architecture of Generated Proxy Code.

The following code demonstrates a simple host item provider that returns an instance of the ShapeApp.Application class. This is the host item provider for the ShapeAppCSharp sample application, with some comments removed for brevity. For more information about this sample, see ShapeApp Samples (Visual Studio Tools for Applications).

internal class HostItemProvider : IHostItemProvider
{
    public HostItemProvider(ShapeApp.Application application)
    {
        this.application = application;
    }

    public object GetHostObject(Type primaryType, string primaryCookie)
    {
        if (primaryType == typeof(ShapeApp.Application))
        {
            return this.application;
        }
        else
        {
            throw new ArgumentOutOfRangeException("primaryType != " + typeof(ShapeApp.Application));
        }
    }

    private ShapeApp.Application application;
}

See Also

Tasks

Walkthrough: Modifying an Application to Load Add-Ins

Concepts

Implementing Host Services

Mapping Host Types to Proxy Types

Discovering and Loading Add-Ins

Considerations for Loading Add-Ins