ITypeResolutionService不解析任何类型

时间:2009-06-22 14:38:20

标签: c# visual-studio visual-studio-2008 extensibility

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

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

1 个答案:

答案 0 :(得分:2)

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

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