2.2 Component Model

Skip to end of metadata
Go to start of metadata

ReSharper has two kinds of components – shell and solution components. Knowledge and understanding of how these differ is critical to being able to write plug-ins that successfully use ReSharper services.

Shell Components

A Shell Component is a component that gets created when the Visual Studio shell is created. Shell components are typically marked with the ShellComponent attribute.

Shell components are useful if you want to use some service in your plug-in that you want to be created virtually as soon as you plug-in is loaded. To do that, simply write:

In certain times, you might need to access other shell components (e.g., ReSharper’s shell components) from within your code. There are, essentially, two ways of doing this.

First, you can use constructor injection, i.e., simply define the component in the constructor of your class and, if the shell component is available, it will be injected automatically:

The other, more straightforward solution, is simply calling Shell.Instance.GetComponent() as follows:

Solution Components

Solution components are components that are tied to the solution currently opened. Consequently, they cannot be accessed when a solution is not present (e.g., in shell components). Solution components use the SolutionComponent attribute.

Solution components are typically acquired through different means compared to shell components. In most cases, you are likely to need a solution component in some method that has been provided, e.g., an IDataContext or even an ISolution. That being the case, you can simply get the solution from this context, and subsequently call GetComponent() on the solution. For example:

Lazy acquisition

In addition to getting the components directly, it's also possible to get them to only be acquired at the time of use. To do this, simply use the type Lazy<ComponentType> instead of ComponentType when querying or injecting a component.

Acquiring VS interfaces

It's important to note that, in addition to ReSharper's own components, you can use the same mechanisms for acquiring the interfaces exposed by Visual Studio itself. For example, to get to VS's Output Window interface IVsOutputWindow, you can acquire it with the following: