1

我正在开发一个 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);
}

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

4

1 回答 1

2

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

这与阻止将泛型类型用作参数的原因相同。如果不指定 T 的实际类型,则不能指定泛型类型。

于 2009-06-22T15:15:24.977 回答