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 2022 - FileExtensionRegistryService returns me UNKNOWN content type for .cs and .txt - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745664362a2162088.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论