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

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

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?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745664363a4639034.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信