简体   繁体   English

如何获取ENVDTE CodeInterface的泛型类型参数?

[英]How to get the generic type parameters for a ENVDTE CodeInterface?

I'm writing a T4 template in Visual Studio 2010 and am generating code based on existing classes in a project. 我正在Visual Studio 2010中编写T4模板,并根据项目中的现有类生成代码。 The code I need to generate depends on the generic type arguments of the interface that the classes implement, but I don't see a way to access that information through the Visual Studio core automation EnvDTE. 我需要生成的代码取决于类实现的接口的泛型类型参数,但我没有看到通过Visual Studio核心自动化EnvDTE访问该信息的方法。 Here is an example of a class that I need to analyse: 以下是我需要分析的类的示例:

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

From this definition I want to generate code (using T4) that looks like this: 从这个定义我想生成代码(使用T4),如下所示:

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

Currently, the code in my T4 template looks a bit like this: 目前,我的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();

But how do I get the generic type arguments of a CodeInterface ? 但是如何获取CodeInterface的泛型类型参数?

It's not pretty, but this does the trick for me: 它不漂亮,但这对我有用:

CodeInterface @interface;

// FullName = "IQuery<[FullNameOfType]>
string firstArgument = @interface.FullName.Split('<', '>')[1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM