2010-05-09 158 views
6

我无法弄清楚这一点。为什么T4没有找到IEnumerable类型?我正在使用Visual Studio 2010.我只希望有人知道为什么?T4故障编译转换

<#@ template debug="true" hostspecific="false" language="C#" #> 
<#@ assembly name="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" #> 
<#@ import namespace="System" #> 
<#@ import namespace="System.Data" #> 
<#@ import namespace="System.Data.SqlClient" #> 
<#@ output extension=".cs" #> 
public static class Tables 
{ 
    <# 

    var q = @" 
     SELECT 
      tbl.name 'table', 
      col.name 'column' 
     FROM 
      sys.tables tbl 
     INNER JOIN 
      sys.columns col ON col.object_id = tbl.object_id 
    "; 

    // var source = Execute(q); 

    #> 
} 
<#+ 
    static IEnumerable Execute(string cmdText) 
    { 
     using (var conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=t4build;Integrated Security=True;")) 
     { 
      conn.Open(); 

      var cmd = new SqlCommand(cmdText, conn); 

      using (var reader = cmd.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
       } 
      } 
     } 
    } 
#> 

错误2编译转型:类型或命名空间名称 'IEnumerable的' 找不到(是否缺少using指令或程序集引用吗?)C:\项目\ T4BuildApp \ T4BuildApp \ TextTemplate1.tt 26 9

回答

3

可能是因为IEnumerable在System.Collections

+1

....约翰做的事情时,你得到更多的权力来referece #assembly NAME =“System.Core程序”和#进口“System.Linq的” - 你不要将'System.Collections'命名空间导入你的模板 – 2010-05-09 19:29:49

+0

哦,我怎么可能没有看到这个。谢谢!我肯定已经失明了一个小时左右... – 2010-05-10 21:33:55

6

我也建议,所以你用了IEnumerable

+1

干杯 - 有一个类似的问题,我错过了一个汇编ref:D – 2011-11-16 16:38:40