9

Visual Studio 2010でT4テンプレートを作成しており、プロジェクト内の既存のクラスに基づいてコードを生成しています。生成する必要のあるコードは、クラスが実装するインターフェイスのジェネリック型引数によって異なりますが、VisualStudioコア自動化EnvDTEを介してその情報にアクセスする方法がわかりません。これが私が分析する必要があるクラスの例です:

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か?

4

1 に答える 1

7

きれいではありませんが、これは私にとってはうまくいきます:

CodeInterface @interface;

// FullName = "IQuery<[FullNameOfType]>
string firstArgument = @interface.FullName.Split('<', '>')[1];
于 2012-10-01T11:58:14.700 に答える