本文介绍了我怎么可以指定T4(文本模板)使用C#4.0,在VS2010?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要指定我T4用C#4.0,以使我的TT文件?我试图用

I need to specify my T4 to use C# 4.0, to render my tt files? I tried using

<#@ template language="C#v4.0" debug="true" #>

但是当我使用一个动态的变量,像这样

But when I use a dynamic variable, like this

dynamic x=10;
Write(x.ToString());

我收到这些错误

I'm getting these errors

Error   2	Compiling transformation: Predefined type 'Microsoft.CSharp.RuntimeBinder.CSharpSetMemberBinder' is not defined or imported	e:\projects\DynamicModel\DynamicModel\ModelGenerator.tt	1	1	
Error   3	Compiling transformation: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpInvokeMemberBinder..ctor'	e:\projects\DynamicModel\DynamicModel\ModelGenerator.tt	1	1	
Error   4	Compiling transformation: Missing compiler required member 'System.Runtime.CompilerServices.CallSite.Create'	e:\projects\DynamicModel\DynamicModel\ModelGenerator.tt	1	1	
Error   5	Compiling transformation: One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?	e:\Projects\DynamicModel\DynamicModel\ModelGenerator.tt	7	8	
Error   6	A namespace cannot directly contain members such as fields or methods	e:\projects\DynamicModel\DynamicModel\ModelGenerator.cs	1	1	DynamicModel

另外,请注意,我用TextTemplatingFileGenerator和 pre处理器模板

Also, please note that I'm using TextTemplatingFileGenerator and Not pre-processor templates

推荐答案

发现,你应该指定正确的组件也是如此。

Found that you should specify the correct assemblies as well.

添加这将确保您使用的是C#4.0。

Adding this will ensure you are using c# 4.0.

<#@ template language="C#" debug="true" #>
<#@ output extension=".txt" #>
<#@ Assembly Name="System.Core, Version=4.0.0.0, Culture=neutral" #>
<#@ Assembly Name="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral" #>

如何以往,出于某种原因,在T4改造时,动态调度仍然没有从T4工作,不知道为什么。从T4它抛出一个运行时错误,同样的code从CS文件执行好直接。

How ever, for some reason, during the time of t4 transformation, the dynamic dispatching is still not working from T4, wondering why. From T4 it throws a runtime error, the same code does good directly from a cs file.

这篇关于我怎么可以指定T4(文本模板)使用C#4.0,在VS2010?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 16:03