Obtain Components in Runtime
ReSharper uses the loosely coupled design. ReSharper's component model provides the ability to live load and unload new components, during runtime.
Typically, you as a plugin developer should use only shell and solution components:
Shell components have the same lifetime as ReSharper and are marked with the
Solution components have the same lifetime with a solution, thus, they are created once a solution is loaded and terminated once the solution is closed. Solution components should be marked with the
So, how could you obtain your own or ReSharper components in runtime? Let's take a look at the most common scenarios.
The easiest way is to use a constructor injection. All you need is to specify the required component as a constructor argument. ReSharper will care about all the dependencies on its own.
For example, you have a solution component
MyClass that should run some action upon creation. To make this happen, it must obtain an
SomeAction is action's name.
Sometimes, you can get components from the
IDataContext objects. This is true, for example, for actions:
Shell and solution component
Another way is to get shell components via the
Shell component - the root API point. This can be helpful, for example, if you want to have some "global" settings and access them from anywhere inside your plugin. To obtain the
Shell instance, you must use its
Thus, you can simply access them from any point of your plugin:
Same is true for solution components. The main difference is that you should have an object that implements the
ISolution interface (the one that refers to the solution currently opened in Visual Studio):