How can I use Linq in a T4 template?

断了今生、忘了曾经 提交于 2019-12-03 04:01:00

问题


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.


回答1:


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. In your template's language attribute, specify "C#v3.5" or "VBv3.5" - this step is not required for VS2010, where .Net 4.0 is always used.
  2. Add an assembly directive for System.Core.dll
  3. Import the System.Linq namespace

Your template will now look something like this:

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

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



来源:https://stackoverflow.com/questions/247005/how-can-i-use-linq-in-a-t4-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!