1

私は、ユーザーがダイアログに入力する一連のオプションと組み合わせたメソッドシグネチャを確認することにより、データアクセスコードを生成するVisualStudio2008アドインに取り組んでいます。

メソッドシグネチャを分析するために、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 に答える