![](/img/trans.png)
[英]How can I get the generic constraints from CodeInterface as a CodeType object?
[英]How to get the generic type parameters for a ENVDTE CodeInterface?
我正在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
的泛型类型参数?
它不漂亮,但这对我有用:
CodeInterface @interface;
// FullName = "IQuery<[FullNameOfType]>
string firstArgument = @interface.FullName.Split('<', '>')[1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.