我正在尝试为CRUD类创建一些脚手架,并希望使用T4来帮助构建这些类.我刚刚开始并且在调用一个住在同一个项目中的类时遇到了问题.例如:
<#@ import namespace="System.Collections.Generic" #>
<#@ template language="C#" #>
<#@ output extension=".cs" #>
<#@ include file="T4Toolbox.tt" #>
using System;
using System.Data;
using System.Data.Linq;
using System.Collections.Generic;
namespace TTFileGenerator
{
<#var entityName = "TEST";#>
public class <#=entityName#>
{
<#
MyClass myClass = new MyClass();
List<string> something = myClass.GetSomething()
...
#>
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
编译转换:找不到类型或命名空间名称"MyClass"(您是否缺少using指令或程序集引用?)
它是同一个项目中的公共类.我可以从项目中的其他类访问它而不是T4文件.还是一个T4新手.任何方向将不胜感激.
我已经堆栈溢出和谷歌搜索一段时间试图找到如何在T4文件中使用Linq扩展方法的答案.我正在使用VS 2012,.NET 4.5.
例如:
Dictionary<string, string> myDict = new Dictionary<string, string>();
myDict = GetSomeData();
Run Code Online (Sandbox Code Playgroud)
让我们说我想得到字典中的最后一项:
<#string last = colNames.Keys.Last();#>
Run Code Online (Sandbox Code Playgroud)
它不介意我使用集合,但它不喜欢".Last()".
编译转换:'System.Collections.Generic.Dictionary.KeyCollection'不包含'Last'的定义,也没有扩展方法'Last'接受类型为'System.Collections.Generic.Dictionary.KeyCollection'的第一个参数' (您是否缺少using指令或程序集引用?)
我已经导入了必要的命名空间......
编辑 - >为了清楚起见......我在T4模板的顶部有以下内容:
<#@ assembly name="$(SolutionDir)\SomeProject\bin\Debug\System.Data.Linq.Dll" #>
<#@ import namespace="System.Data.Linq" #>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?提前致谢.