如何在T4模板中使用Linq? [英] How can I use Linq in a T4 template?

查看:98
本文介绍了如何在T4模板中使用Linq?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用T4为项目生成一些屏幕和中间层代码,并且希望使用Linq来简化我的一些模板代码.但是,当我尝试使用Linq时,模板会报告语法错误.

I am using T4 to generate some screens and middle-tier code for a project, and would like to use Linq to simplify some of my template code. However, when I try to use Linq, the template reports a syntax error.

推荐答案

默认情况下,在Visual Studio 2008中(和大多数在线示例中所使用的一样),该模板是使用2.0框架编译的,该框架不包含Linq. (请参阅 MSDN论坛线程)

By default in Visual Studio 2008 (and as used in most online examples) the template is compiled with the 2.0 Framework, which does not include Linq. (See MSDN forum thread)

要解决此问题,需要三个步骤:

To solve the problem, three steps are needed:

  1. 在模板的语言属性中,指定"C#v3.5"或"VBv3.5"-对于始终使用.Net 4.0的VS2010,此步骤不是必需的.
  2. 为System.Core.dll添加汇编指令
  3. 导入System.Linq命名空间

您的模板现在将如下所示:

Your template will now look something like this:

<#@ template language="C#v3.5" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>

您现在可以在模板中使用Linq和其他新语言功能.

You can now use Linq and other new language features in your template.

这篇关于如何在T4模板中使用Linq?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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