admin管理员组

文章数量:1026989

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?

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?

本文标签: visual studio 2022FileExtensionRegistryService returns me UNKNOWN content type for cs and txtStack Overflow