2012-09-30 15 views
9

私はVisual Studio 2010でT4テンプレートを作成しており、プロジェクトの既存のクラスに基づいてコードを生成しています。私が生成する必要があるコードは、クラスが実装するインターフェイスのジェネリック型の引数に依存しますが、Visual StudioのコアオートメーションEnvDTEを通じてその情報にアクセスする方法はありません。ここで私は分析する必要があるクラスの例です。ENVDTE CodeInterfaceのジェネリック型パラメータを取得するには?

public class GetCustomerByIdQuery : IQuery<Customer> 
{ 
    public int CustomerId { get; set; } 
} 

この定義から、私は(T4を使用して)コードを生成したい、このように見えること:

[OperationContract] 
public Customer ExecuteGetCustomerByIdQuery(GetCustomerByIdQuery query) 
{ 
    return (Customer)QueryService.ExecuteQuery(query); 
} 

現在、私のコードT4テンプレートは、ビットのようになります。

CodeClass2 codeClass = GetCodeClass(); 

CodeInterface @interface = codeClass.ImplementedInterfaces 
    .OfType<CodeInterface>() 
    .FirstOrDefault(); 

// Here I want to do something like this, but this doesn't work: 
// CodeClass2[] arguments = @interface.GetGenericTypeArguments(); 

しかし、どのように私はCodeInterfaceのジェネリック型引数を得るのですか?

+0

なぜ 'タイプが[]の型を=()'? –

+0

@Cuong:そして、インターフェイスのTypeインスタンスをどうやって得るのですか? Visual Studio interopは 'Type'ではなく' CodeClass'インスタンスで動作することを忘れないでください。 – Steven

+1

私は同じ問題を抱えていますが、ImplementedInterfacesのカウントが0であるという点で悪いです。クラス実装でジェネリックスを取得するには、より良い方法が必要です。 –

答えて

6

それはかなりありませんが、これは私のためのトリックを行います。interface.GenericTypeArguments @

CodeInterface @interface; 

// FullName = "IQuery<[FullNameOfType]> 
string firstArgument = @interface.FullName.Split('<', '>')[1]; 
関連する問題