Visual Studio SDK获取类型修饰符信息-类型是抽象的还是内部的? [英] Visual Studio SDK get type modifier information - is type abstract or internal?

查看:143
本文介绍了Visual Studio SDK获取类型修饰符信息-类型是抽象的还是内部的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用

I use the IVsObjectList2.GetCategoryField2 method to retrieve different information of a type.

现在我想知道如何检索C#特定信息,例如类型的abstractinternal修饰符? 对象浏览器可以显示此信息.

Now I wonder how I can retrieve C# specific information like abstract or internal modifier of a type? The Object Browser can displays this informations.

更新1:

我已经做了另一次尝试来获取此信息.通过项目的DynamicTypeServiceIVsHierarchy,我可以获得TypeResolutionService.然后,这可以返回我正在寻找的Type,并形成我得到信息(内部,抽象等)的Type

I have made another attempt to get this information. Via the DynamicTypeService and the IVsHierarchy (of the project) I can get the TypeResolutionService. This can then return the Type I'm are looking for, and form the Type I get the infomrations (internal, abstract, etc.)

不幸的是,这仅适用于.NET Framework项目. .NET Core项目不起作用.假定.NET核心项目在解决时会引起问题,因为VS加载项(或Visual Studio SDK)在.NET Framework下运行.

Unfortunately, this only works with .NET Framework projects. .NET Core projects don't work. The assumption is that .NET core projects cause problems when resolving, because the VS add-in (or Visual Studio SDK) run under .NET Framework.

var dte = Package.GetGlobalService(typeof(DTE)) as DTE2;
var serviceProvider = new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)dte);

IVsSimpleObjectList2 objectList;
....
objectList.CountSourceItems(index, out var vsHierarchy, out var itemid, out var pcItems);

DynamicTypeService dynamicTypeService = (DynamicTypeService)serviceProvider.GetService(typeof(DynamicTypeService));
var typeResolutionService = dynamicTypeService.GetTypeResolutionService(hier);
var type = typeResolutionService.GetType("ObjectBuilder.ObjectBrowserTestTypes.AbstractTestClass");

此处有更多信息:Visual Studio Extension获取所有类和接口元数据

我仍在寻找解决方案.有人有其他想法吗?

I'm still looking for a solution. Does anyone have another idea?

推荐答案

最后,我决定不通过对象浏览器或IVsObjectManager2检索有关类型的信息.原因是我没有获得所需的全部信息.

At the end, I decided not to retrieve the information about the types via the Object-Browser or IVsObjectManager2. The reason is that I didn't get all the information I needed.

对于当前加载的Visual Studio项目中的类型,我使用的是ElementClassCodeClass类.

For types in the currently loaded visual studio projects I'm using the ElementClass or CodeClass class.

var service = Package.GetGlobalService(typeof(DTE)) as DTE2;
Project project = service?.Solution?.Projects.Item(0);
CodeType codeType = project.CodeModel.CodeTypeFromFullName("Full name of Type");

if (codeType.Kind == vsCMElement.vsCMElementClass && codeType is CodeClass2 codeClass)
{
    // get all the information form the code class
    var typeDescription = new TypeDescription();
    typeDescription.FullName = codeClass.FullName;
    typeDescription.ContainsGenericParameters = codeClass.IsGeneric;
    typeDescription.IsAbstract = codeClass.IsAbstract;
}

对于引用的程序集中的类型,我正在使用Mono.Cecil. Mono.Cecil的优点是,它可以与.NET Framework DLL和.NET Core DLL一起使用.引用的程序集的路径可以通过VS-SDK获取.

For types that are in a referenced assembly I'm using Mono.Cecil. The advantage of Mono.Cecil is, that it works with .NET Framework DLLs and .NET Core DLLs. The path of the referenced assembly can be gotten via the VS-SDK.

var vsProject = project.Object as VSLangProj.VSProject;
var assemblyPath = vsProject.References.Item(0).Path;

ModuleDefinition module = Mono.Cecil.ModuleDefinition.ReadModule(assemblyPath);
foreach (TypeDefinition type in module.Types)
{
    var isAbstract = type.IsAbstract;
}

这篇关于Visual Studio SDK获取类型修饰符信息-类型是抽象的还是内部的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆