我正在使用Visual Studio 2010编写T4模板,根据项目中现有的类生成代码。我需要生成的代码取决于这些类实现的接口的泛型类型参数,但我没有看到通过Visual Studio核心自动化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
的泛型类型参数?
Type[] types = @interface.GenericTypeArguments()
呢? - cuongle