关于 T4 模板的学术查询 [英] An academic query about T4 templates

查看:30
本文介绍了关于 T4 模板的学术查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,在我目前的项目中,我想得有点提前,所以我为这个问题的含糊不清表示歉意.在解决方案的一个项目中,是否有可能拥有一个 T4 模板,该模板引用解决方案中的其他程序集之一并检查其他程序集的导出类型并使用它们的元数据来构建其他代码?我不需要引用该程序集目录中的类型,我只需要能够获取从特定基类派生的所有类型的列表以及有关它们的一些元数据.我正在考虑专门针对一些从公共基类构建并由 NHibernate 转储到数据库的对象执行此操作,作为从它们生成 DTO 类以便在 AJAX 调用期间抛出客户端的简单方法.我不是在寻找一个绝对完美的解决方案,只是寻找一个能够解决很多简单问题的解决方案.

Ok, I'm thinking a little ahead here in my current project, so I apologize for how vague this question is going to be. Is it possible in one project of a solution to have a T4 template that references one of the other assemblies in the solution and inspects the exported types of the other assembly and uses their metadata to build up other code? I don't need to reference the types in that assembly directory, I just need to be able to get a list of all of them that derive from a particular base class and some metadata about them. I'm thinking of doing this specifically for some objects that are built up from a common base class and dumped to the database by NHibernate as an easy way to generate DTO classes from them for throwing at the client during AJAX calls. I'm not looking for an absolutely perfect solution, just one that gets a lot of the easy cases out of the way.

再说一次,我真的没有一个具体的例子.我距离正面遇到这个问题还有几天的时间,但我突然想到这个选项可能涵盖我可能遇到的很多情况.

Again, I don't really have a specific example. I'm a few days out from running into this problem head-on, but it occurred to me that this option might cover a lot of the cases I might run into.

想法?

推荐答案

我试图实现类似的目标,并且效果很好.在下面的示例 .tt 文件中,我所指的同一解决方案中我自己的程序集的名称是 SomeLibrary.我正在思考的类是一个名为 SomeLibrary.SomeInterface 的接口.有很多方法可以使这更通用,但我不想让示例太复杂.另外,请不要关注这个模板的整体结果,这只是为了让您对它的工作方式有一个印象:

I was trying to achieve something similar and it works fine. In the example .tt file below, the name of my own assembly in the same solution that I am referring to is SomeLibrary. The class I am reflecting on is an interface called SomeLibrary.SomeInterface. There are ways to make this more generic, but I did not want to make the sample too complicated. Also, please do not pay attention to the overall result of this template, this is just to give you an impression on how it would work:

<#@ template language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="$(SolutionDir)$(SolutionName)\bin\$(ConfigurationName)\$(SolutionName).dll" #>
<#@ import namespace="System.Reflection" #>
<#
    Type someIType = typeof(SomeLibrary.SomeInterface);
#>
namespace SomeLibrary
{
    class <#=someIType.Name#>Impl: <#=someIType.Name#>
    {
        <#=someIType.Name#> encapsulatedIntf;
        public <#=someIType.Name#>Impl(<#=someIType.Name#> anIntf)
        {
            encapsulatedIntf = anIntf;
        }

        // Methods to be implemented:
<#
    MethodInfo[] methods = someIType.GetMethods();
    foreach (MethodInfo m in methods)
    {
#>
        // <#=m#>;
<#
    }
#>
    }
}

就我而言,这会导致

namespace SomeLibrary
{
    class SomeInterfaceImpl: SomeInterface
    {
        SomeInterface encapsulatedIntf;
        public SomeInterfaceImpl(SomeInterface anIntf)
        {
            encapsulatedIntf = anIntf;
        }

        // Methods to be implemented:
        // Int32 someMethod(Int32);
        // System.String someOtherMethod();
    }
}

这篇关于关于 T4 模板的学术查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆