[英]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.