最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

visual studio 2022 - FileExtensionRegistryService returns me UNKNOWN content type for .cs and .txt - Stack Overflow

programmeradmin1浏览0评论

In the Visual Studio Extension development scenario, I try to get content types as follows.

var componentModel = (IComponentModel)Package.GetGlobalService(serviceType: typeof(SComponentModel));

var fileExtensionRegistryService = componentModel.GetService<IFileExtensionRegistryService>();

var extensionString = "cs";

var contentTypeForGivenExtension = fileExtensionRegistryService.GetContentTypeForExtension(extension: extensionString);

extensionString = "txt";

contentTypeForGivenExtension = fileExtensionRegistryService.GetContentTypeForExtension(extension: extensionString);

The contentTypeForGivenExtension.DisplayName for both the cs as well txt cases above, is UNKNOWN. Why is that? Am I missing something here?

Also I dont get any extensions for CSharp and text content types in the following.

var contentTypeRegistryServiceLocal = componentModel.GetService<IContentTypeRegistryService>();

// I get a total of 95 content types here.
var contentTypeList = contentTypeRegistryServiceLocal.ContentTypes.
    OrderBy(keySelector: contentType => contentType.TypeName).ToList();

// I get the text Content Type correctly here. 
var textContentType = contentTypeList.
    Where(predicate: contentType => contentType.DisplayName.Equals("text")).First();

// I get the extensionList count as 0 here for text content type. Not sure why
var extensionListForTextContentType = fileExtensionRegistryService.
    GetExtensionsForContentType(contentType: textContentType).ToList();

// I get the CSharp Content Type correctly here. 
var cSharpContentType = contentTypeList.
    Where(predicate: contentType => contentType.DisplayName.Equals("CSharp")).First();

// I get the extensionList count as 0 here for CSharp content type. Not sure why
var extensionListForCSharpContentType = fileExtensionRegistryService.
    GetExtensionsForContentType(contentType: cSharpContentType).ToList();

So what am I missing here?

If you want to take a look at the full Visual Studio solution, its here. And the command file is this.

There is more.

Instead of cs and txt, if I register a custom content type, then it works.

Take a look at this example here.

I define a FooAbcd ContentType here.

public class FooAbcdContentDefinition
{
    public const string ContentTypeName = "FooAbcd";
    // Comment out the following eight lines, to test content types registration.
    [Export]
    [Name(ContentTypeName)]
    [BaseDefinition(CodeRemoteContentDefinition.CodeRemoteContentTypeName)]
    internal static ContentTypeDefinition FooContentTypeDefinition;
    [Export]
    [FileExtension(".FooAbcd")]
    [ContentType(ContentTypeName)]
    internal static FileExtensionToContentTypeDefinition FooFileExtensionDefinition;
}

And from the IFileExtensionRegistryService I get the FooAbcd ContentType as follows.

var contentTypeRegistryService = componentModel.GetService<IContentTypeRegistryService>();

var contentTypeList = contentTypeRegistryService.ContentTypes.
    OrderBy(contentType => contentType.TypeName).ToList();
    
var fooAbcdContentType = contentTypeList.Where(contentType => contentType.DisplayName == FooAbcdContentDefinition.ContentTypeName).FirstOrDefault();

// Then I get the fileExtensionRegistryService
var fileExtensionRegistryService = componentModel.GetService<IFileExtensionRegistryService>();

var extensionListForFooAbcdContentType = fileExtensionRegistryService.GetExtensionsForContentType(fooAbcdContentType).ToList();

Now I get the file extension from the content type as follows.

// Then I get the fileExtensionRegistryService
var fileExtensionRegistryService = componentModel.GetService<IFileExtensionRegistryService>();

var extensionListForFooAbcdContentType = fileExtensionRegistryService.GetExtensionsForContentType(fooAbcdContentType).ToList();

And extensionListForFooAbcdContentType contains the extension. Run the example available at my github

So the the problem is only with cs and txt. But things work correctly with custom content type.

What am I missing?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论