Previous articlesArticleIn, we just managed to gain a glimpse of vsx by creating and analyzing three very small packages generated by the vspackage wizard. These examples help us to familiarize ourselves with the basic steps for creating a small package. However, we must take a deeper look at how visual studo ide works and how it integrates package.
Before we discuss other details, we should first sort out our understanding of vsx. In this article, we will not create anyCodeTo clarify the concepts related to vsx.
Warning "the underlying layer uses com"
I have mentioned many times in the previous articles that Visual Studio's extensibility development is based on the COM technology. Objects and entities (such as commands, menus, toolbar, windows, editors, and projects) in a package are COM objects. Of course, if we use managed code (such as C # and VB. NET), we can see that these classes and instances are managed. net types and instances. But if we use unmanaged code, we have to process COM objects and instances.
During vsx code development, many modes and features can be used because vsx uses com both in and out. I suppose you do not have a deep understanding of COM (I am not a com expert myself), but I will tell you some basic knowledge that must be understood later.
What is Visual Studio package?
In the previous articles, we have created several simple Visual Studio packages, so we have a preliminary understanding of vspackage. Now let's take a deeper look at it.
Vspackage is a basic unit for building Visual Studio. In fact, Visual Studio is made up of a series of vspackage collaborative work, just like an ecosystem. A package is a basic unit of Vs in terms of its architecture, deployment, security, and license authentication. In addition, one or more packages can exist physically in the sameProgramCentralized.
Developers (including Visual Studio developers) can expand vs ide by creating vspackage. These extensions can be:
- Service ).Services are some objects that provide functions for developers or other package calls. For example, the C # Language Service (as the name suggests) is a service.
- Interface Element.For example, menus, toolbar, and window, developers can use them to execute some actions on the user interface to display messages, information, images, and so on.
- Editor.During the development process, we compile a program to create an application. The editor is responsible for writing programs. Visual Studio 2008 has its own core editor, but we can also create our own editor in vspackage.
- Designer.Application creation is not just as simple as typing text. We have a lot of visualization tools known as the designer that we can use to design modules, components, parts, and even the entire application system. The famous example is the winform designer. We can use it to create the winform user interface.
- Project.When developing an application, we generally target a large number of files. A project is used to organize these source files and resources. It is not as simple as simply storing these files. It can also be used to compile, debug, and publish products created by source files.
In the subsequent articles, we will discuss the details of these extensions one by one. Here I will give you a basic overview to illustrate what they are and how they are used in.
In addition, a package can display its own information in the Visual Studio startup interface or in the dialog box.
A package can save its status and configuration information in a persistent storage device and read these configurations. For example, you can set syntax highlighting, Font, color, and labels in a text editor.
Each package must be signed by the so-called package load key (PLK). Visual Studio uses it to check the validity of the package. Visual Studio only loads a package with a valid PLK. In addition, technically, the package is implementedIvspackageInterface type. This time we will not discuss ivspackage in depth, but in subsequent articles we will test its details through code.
What is a service )?
Generally, we do not develop a package to develop a package. We create packages because they not only provide functions for ourselves (in this case, we are consumers), but also provide functions for other packages (in this case, other packages are consumers ). For example, assume that our package provides a tool window to find reference to a specific method, and we are the consumer of this window. If this package not only provides the search function for this tool window, but also serves as a "callable method" for other package services, other packages are the consumers of this service.
Therefore, a service is a package or package and its related objects (when I say "package object, I mean the contract between windows, commands, designers, and other things created by the package itself.
Describes the concept between vspackage and service:
(Translator's note: I am very sorry, there is a picture missing here. The image link in the original text is invalid. I contacted the original text author but did not respond to it. If I find this image later, I must add it .)
Vspackage can contain services, which are called service providers. In,Vspackage1AndVspackage3Is the service provider, andVspackage2No. A Global Service (Global Service). Vspackage1 and vspackage3 both contain global services, which can be called by vspackage2 (or other packages ). A package can also contain services that can only be called by itself or can only be called by package objects. This service is called a local service (Local Service). Both vspackage1 and vspackage3 contain local services, which are called by objects (for example, by the editor in vspackage1 and the tool window in vspackage3 ).
Use Service
There is a bad message about services in vsx: they are hidden and cannot be easily discovered. This means we cannot guess which services can be provided in a package (or other objects.
Therefore, if you want to use a service, you must "call it by its name", which means you must know the name of the service. The only way to know the service name is to check the documentation provided by the package where the service is located. The vsx document lists about 130 services.
Generally, a service is defined as an interface. Most services only implement one interface, but some services implement multiple interfaces. Therefore, when we want to use a service, we must know two "names": Service name and interface name.
You may have noticed that I use quotation marks for "name. This is because all services are objects. If we use the InterOP type, "names" are their. net type. If we use a COM Object (unmanaged code), the "name" is the guid of these COM types.
Let's use an example to illustrate it more clearly! InSimplecommand, We useSvsuishellService to display a message box, we useGetserviceMethod to obtainIvsuishellInterface reference:
1:Ivsuishell uishell = (ivsuishell) getservice (Typeof(Svsuishell ));
2:Uishell. showmessagebox (...);
When we get a service reference, we can use the methods and attributes it provides. In the above example, we call this service instanceUishellThe showmessagebox method.
Load package and Access Service
The way vspackage accesses the Service (now I only mean getting the service instance through the getservice method) brings some "Features" and results to the service model.
- Load as needed.If a package service is not used, it does not need to be loaded into the memory. Therefore, vs ide only loads the packages called by those services ). In fact, package and service provider are two different concepts. Theoretically, any object that implements iserviceprovider can provide services. If we inherit the abstract classPackage(As in the previous articles), our package will automatically become a service provider because the base class package implements the iserviceprovider interface and many other interfaces.
- Siting vspackages.IDE provides a service provider for all vspackage, which can be used to obtain service instances provided by other packages or service providers. When a package is loaded into the memory, Visual Studio passes a reference to a service provider of the package. You can use this reference to access the global service ). Therefore, we call it siting vspackage. If a package is not stored in the site, it cannot interact with Visual Studio through the service instance.
- Access Global Service ).If we have a site object (such as a vspackage or an object that implements the ivspackage Interface), we can easily use the getservice method to access the global service; if our object is not site, but we can use it to get the vspackage instance where it is located, we can still use the getservice method. However, in some cases, our object is not put on the site and we cannot get the package instance. In this case, we can usePackage. getglobalserviceThis static method.
Registration Service
After learning so much about the service, if I tell you that you must register them before using the service, I guess you will not be surprised.
To register a service, you must addProvideserviceattribute.Regpkg.exeThis attribute is used to register the service.
Although there are a lot of interesting things about services, this is enough for the moment. In the future, we will study it through code.
Interoperability assembly and managed package framework
. NET developers prefer managed. Net types because they can take advantage of the powerful features of the underlying runtime environment. However, for historical reasons (vs versions earlier than the. NET era), the main part of Visual Studio is built on unmanaged code and supports COM classes and interfaces. To access COM objects,. NET provides something called interoperability Assembly. In short, it uses the. Net type to encapsulate the com type. We have two main methods to use the COM Object in vsx: Creating unmanaged code (for example, using C ++ ); or use the interoperability assembly to write managed code (using C # Or VB.. net ).
For me, I prefer to host code (and I guess most. NET developers do), so I will use the InterOP assembly for my sample code. In some common tasks, com uses different modes, such as type identification, memory allocation, and exception management. In addition, COM does not support inheritance.
If we only use the InterOP assembly, our code will become very lengthy and cannot use the language and some runtime functions provided by. NET and C. Microsoft created a framework named managed package framework (MPF) on the com interoperability assembly of Visual Studio, which can help us to create vspackage with localized hosting code.
InterOP assembly in vsx
A bunch of vsx InterOP assembly is installed in GAC. You can also installC: \ Program Files \ Microsoft Visual Studio 2008 SDK) UnderVisualstudiointegration \ common \ assembliesFind them in the subdirectory. These InterOP Assemblies start with Microsoft. visualstudio, but not all assemblies starting with this are InterOP assemblies. In this folder, you can see almost 100 assembly files. The InterOP assembly is as follows (I omittedMicrosoft. visualstudioPrefix ):
Assembly |
Description |
~. Shell. InterOP |
This Assembly defines hundreds of core InterOP types (including interfaces, structures, enumerations, classes, and so on) |
~. Shell. interop.8.0 ~. Shell. interop.9.0 |
In VS 2005 and vs 2008, different COM types are defined in these two sets, of which 8.0 is used for VS 2005, 9.0 is used for vs 2008. |
~. Ole. InterOP |
This Assembly encapsulates hundreds of standard Ole types. |
~. Textmanager. InterOP ~. Textmanager. interop.8.0 |
Visual Studio has a good built-in editor. These two sets are used to access the Editor interface. Among them, 8.0 is for the new interface types in Visual Studio 2005 and 2008. |
~. Debugger. InterOP |
If you want to access the interfaces and debugging functions in the built-in debugger provided by vs IDE, you can use this assembly. |
Assembly in managed package framework
the MPF assembly and InterOP assembly (and other vsx-related Assembly) are in the same folder and begin with Microsoft. visualstudio . The most important assembly is as follows:
Assembly |
description |
~. Shell and ~. Shell.9.0 |
these two sets define the core types of MPF. The Assembly ending with 9.0 is for vs 2008. If you use vs 2008, you should use this Assembly to register your compiled package. |
~. Shell. Design (Translator's note: ~. Shell. desing should be a clerical error.) |
types defined in this set can be used to extend the Visual Studio designer. |
|
|
|
|
|
|
Assembly to be referenced in vspackage
If you use vs 2008 to create a new vspackage, The Wizard will add some references to the InterOP assembly and MPF Assembly. These references include:
- Microsoft. visualstudio. Ole. InterOP
- Microsoft. visualstudio. shell.9.0
- Microsoft. visualstudio. Shell. InterOP
- Microsoft. visualstudio. Shell. interop.8.0
- Microsoft. visualstudio. Shell. interop.9.0
- Microsoft. visualstudio. textmanager. InterOP
If you need other InterOP or MPF assembly, you can add reference by yourself.
Summary
In this article, we have discussed the basic concepts and most important details of vsx.
- Vspackage is the basic unit in Visual Studio infrastructure, security, deployment, and license authentication. Visual Studio is also built on a series of vspackage.
- Vspackage can provide global services for other packages. Services are hidden and hard to find. You must register them before using them; visual Studio provides an On-Demand Loading Mode for searching and loading service providers.
- Vspackage is based on COM technology. Visual Studio provides an InterOP assembly to access the com type. MPF (managed package framework) encapsulates the InterOP assembly and allows the development of a package with localized hosted code.
Of course, there are many other important concepts in vsx, but it is sufficient for us to continue our learning.
In the next article, we will continue to use code examples to explore vsx development.
Link: http://dotneteers.net/blogs/divedeeper/archive/2008/01/11/LearnVSXNowPart5.aspx