ITypeResolutionService 未解析任何类型

发布于 2024-07-25 08:20:51 字数 1331 浏览 8 评论 0原文

我正在开发一个 Visual Studio 2008 插件,它将通过查看方法签名以及用户在对话框中输入的一组选项来生成数据访问代码。

为了分析方法签名,我使用 Visual Studio 的 ITypeResolutionService 来查找当前项目、引用的项目或引用的程序集中存在的类型。

为此,我创建了以下功能:

private ITypeResolutionService _typeResolutionService;
private ITypeDiscoveryService _typeDiscoveryService;

/// <summary>
/// Initializes a new instance of the TypeResolver class.
/// </summary>
public TypeResolver(VisualStudioServiceProvider serviceProvider, Project project)
{
    IVsSolution solution = serviceProvider.GetService<IVsSolution>();
    DynamicTypeService typeResolver = serviceProvider.GetService<DynamicTypeService>();

    IVsHierarchy hierarchy = null;
    solution.GetProjectOfUniqueName(project.UniqueName, out hierarchy);

    _typeResolutionService = typeResolver.GetTypeResolutionService(hierarchy);
    _typeDiscoveryService = typeResolver.GetTypeDiscoveryService(hierarchy);
}

/// <summary>
/// Resolves a type in the current solution
/// </summary>
/// <param name="name">Name of the type to resolve</param>
/// <returns>Returns the resolved type; otherwise null</returns>
public Type Resolve(string name)
{
    return _typeResolutionService.GetType(name, true);
}

它确实可以解析非泛型类型,但遗憾的是不适用于泛型类型。 有人知道如何让上面的代码片段也适用于泛型类型吗?

I'm working on a visual studio 2008 add-in that will generate data-access code by looking at the method signature combined with a set of options the user enters in a dialog.

For analyzing the method signature I use the ITypeResolutionService of visual studio to lookup a type that either exists in the current project, in the referenced projects or in the referenced assemblies.

For this I created the following functionality:

private ITypeResolutionService _typeResolutionService;
private ITypeDiscoveryService _typeDiscoveryService;

/// <summary>
/// Initializes a new instance of the TypeResolver class.
/// </summary>
public TypeResolver(VisualStudioServiceProvider serviceProvider, Project project)
{
    IVsSolution solution = serviceProvider.GetService<IVsSolution>();
    DynamicTypeService typeResolver = serviceProvider.GetService<DynamicTypeService>();

    IVsHierarchy hierarchy = null;
    solution.GetProjectOfUniqueName(project.UniqueName, out hierarchy);

    _typeResolutionService = typeResolver.GetTypeResolutionService(hierarchy);
    _typeDiscoveryService = typeResolver.GetTypeDiscoveryService(hierarchy);
}

/// <summary>
/// Resolves a type in the current solution
/// </summary>
/// <param name="name">Name of the type to resolve</param>
/// <returns>Returns the resolved type; otherwise null</returns>
public Type Resolve(string name)
{
    return _typeResolutionService.GetType(name, true);
}

It does resolve non-generic types, but sadly doesn't work on generic types.
Does anybody have an idea on how to get the above snippet working for generic types too?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兲鉂ぱ嘚淚 2024-08-01 08:20:51

泛型类型的类型是其类型参数在运行时的类型。 在设计时,泛型类型没有类型,因为参数化类型尚未指定。 它可能不起作用,因为调用 GetType 时不存在类实例。

这与阻止泛型类型用作参数的推理相同。 如果不指定 T 的实际类型,则无法指定泛型类型。

The type of a generic type is the type of its type parameter at runtime. At design time, a generic type has no type, because the parameterized type has not been specified. It's probably not working because no class instance exists at the time GetType is called.

This is the same reasoning that prevents generic types from being used as arguments. You cannot specify a generic type without specifying the actual type of the T.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文