Freigeben über


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 2010 SDK. For more information, see Extending Visual Studio Overview. To find out how to download the Visual Studio SDK, see Visual Studio Extensibility Developer Center on the MSDN Web site.

Creating a MEF Project

To create a MEF project

  1. Create a C# or Visual Basic Editor Classifier project. Name the solution ContentTypeTest.

  2. Open the source.extension.vsixmanifest file in the VSIX Manifest Editor.

  3. Make sure that the Content heading contains a MEF Component content type and that the Path is set to ContentTypeTest.dll.

  4. Save and close source.extension.vsixmanifest.

  5. Delete the existing class files.

Defining the Content Type

To define a content type

  1. Add a class file and name it FileAndContentTypes.

  2. Add the following using directives (Imports statements in Visual Basic).

    Imports System.ComponentModel.Composition
    Imports Microsoft.VisualStudio.Text.Classification
    Imports Microsoft.VisualStudio.Utilities
    
    using System.ComponentModel.Composition;
    using Microsoft.VisualStudio.Text.Classification;
    using Microsoft.VisualStudio.Utilities;
    
  3. Declare a static class (Module in Visual Basic) that contains the definitions.

    Friend Module FileAndContentTypeDefinitions
    
    End Module
    
    internal static class FileAndContentTypeDefinitions
    
  4. In this class, export a ContentTypeDefinition named "hid" and declare its base definition to be "text".

    <Export(), Name("hid"), BaseDefinition("text")>
    Friend myDefinition1 As ContentTypeDefinition
    
    [Export]
    [Name("hid")]
    [BaseDefinition("text")]
    internal static ContentTypeDefinition hidingContentTypeDefinition;
    

Linking a File Name Extension to a Content Type

  • To map this content type to a file name extension, export a FileExtensionToContentTypeDefinition that has the extension ".hid" and the content type "hid".

    <Export(), FileExtension(".hid"), ContentType("hid")>
    Friend myDefinition2 As FileExtensionToContentTypeDefinition
    
    [Export]
    [FileExtension(".hid")]
    [ContentType("hid")]
    internal static FileExtensionToContentTypeDefinition hiddenFileExtensionDefinition;
    

Adding the Content Type to an Editor Export

To add the content type to an editor extension

  1. Create an editor extension.

  2. When you export it, add a ContentTypeAttribute of type "hid" to it.

    <Export(), ContentType("hid")>
    Friend myDefinition3 As FileExtensionToContentTypeDefinition
    
    [Export]
    [ContentType("hid")]
    

See Also

Concepts

Editor Extension Points