Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Walkthrough: Linking a Content Type to a File Name Extension

You can define your own content type and link a file name extension to it by using editor Managed Extensibility Framework (MEF) extensions. In some cases, the file name extension has already been defined by a language service; nevertheless, to use it with MEF you still must link it to a content type.

To follow this walkthrough, you must install the Visual Studio SDK. For more information, see Visual Studio SDK.

  1. Create a C# VSIX project. (In the New Project dialog, select Visual C# / Extensibility, then VSIX Project.) Name the solution ContentTypeTest.

  2. 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.

  1. Add a class file and name it FileAndContentTypes.

  2. Add references to the following assemblies:

    1. System.ComponentModel.Composition

    2. Microsoft.VisualStudio.Logic

    3. Microsoft.VisualStudio.CoreUtility

  3. Add the following using directives.

    using System.ComponentModel.Composition;
    using Microsoft.VisualStudio.Text.Classification;
    using Microsoft.VisualStudio.Utilities;
    
  4. Declare a static class that contains the definitions.

    internal static class FileAndContentTypeDefinitions
    {. . .}
    
  5. In this class, export a 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 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;
    }
    

  1. Create an editor extension. For example, you can use the margin glyph extension described in Walkthrough: Creating a Margin Glyph.

  2. Add the class you defined in this procedure.

  3. When you export the extension class, add a ContentTypeAttribute of type "hid" to it.

    [Export]
    [ContentType("hid")]
    
Show:
© 2015 Microsoft