title | description | ms.date | ms.topic | helpviewer_keywords | author | ms.author | manager | ms.subservice | |
---|---|---|---|---|---|---|---|---|---|
Link a content type to a filename extension |
Learn how to link your own content type to a file name extension by using the editor Managed Extensibility Framework extensions in this walkthrough. |
11/04/2016 |
how-to |
|
maiak |
maiak |
mijacobs |
extensibility-integration |
You can define your own content type and link a file name extension to it by using the editor Managed Extensibility Framework (MEF) extensions. In some cases, the file name extension is already defined by a language service. But, to use it with MEF, you must still link it to a content type.
-
Create a C# VSIX project. (In the New Project dialog, select Visual C# / Extensibility, then VSIX Project.) Name the solution
ContentTypeTest
. -
In the source.extension.vsixmanifest file, go to the Assets tab, and set the Type field to Microsoft.VisualStudio.MefComponent, the Source field to A project in current solution, and the Project field to the name of the project.
-
Add a class file and name it
FileAndContentTypes
. -
Add references to the following assemblies:
-
System.ComponentModel.Composition
-
Microsoft.VisualStudio.Text.Logic
-
Microsoft.VisualStudio.CoreUtility
-
-
Add the following
using
directives.using System.ComponentModel.Composition; using Microsoft.VisualStudio.Text.Classification; using Microsoft.VisualStudio.Utilities;
-
Declare a static class that contains the definitions.
internal static class FileAndContentTypeDefinitions {. . .}
-
In this class, export a xref:Microsoft.VisualStudio.Utilities.ContentTypeDefinition named "hid" and declare its base definition to be "text".
internal static class FileAndContentTypeDefinitions { [Export] [Name("hid")] [BaseDefinition("text")] internal static ContentTypeDefinition hidingContentTypeDefinition; }
-
To map this content type to a file name extension, export a xref:Microsoft.VisualStudio.Utilities.FileExtensionToContentTypeDefinition that has the extension .hid and the content type "hid".
internal static class FileAndContentTypeDefinitions { [Export] [Name("hid")] [BaseDefinition("text")] internal static ContentTypeDefinition hidingContentTypeDefinition; [Export] [FileExtension(".hid")] [ContentType("hid")] internal static FileExtensionToContentTypeDefinition hiddenFileExtensionDefinition; }
-
Create an editor extension. For example, you can use the margin glyph extension described in Walkthrough: Create a margin glyph.
-
Add the class you defined in this procedure.
-
When you export the extension class, add a xref:Microsoft.VisualStudio.Utilities.ContentTypeAttribute of type "hid" to it.
[Export] [ContentType("hid")]