Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify T4 (Text Templates) to use C# 4.0, in VS2010?

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

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

like image 862
amazedsaint Avatar asked Oct 07 '09 11:10

amazedsaint


People also ask

What are T4 templates in Entity Framework?

T4 templates in entity framework are used to generate C# or VB entity classes from EDMX files. Visual Studio 2013 or 2012 provides two templates- EntityObject Generator and DBContext Generator for creating C# or VB entity classes. The additional templates are also available for download.

What is transform all T4 templates?

Transform All T4 Templates searches your solution for *. tt files and executes them to create other text, again typically source code, files.


2 Answers

Found that you should specify the correct assemblies as well.

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" #>

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.

like image 111
amazedsaint Avatar answered Oct 10 '22 11:10

amazedsaint


I believe that by default the T4 engine can only use 4.0, although I'd make sure that any DLLs you reference are correct for your error.

like image 27
Colin Asquith Avatar answered Oct 10 '22 11:10

Colin Asquith