首页 新闻 赞助 找找看

t4 模板的问题

0
悬赏园豆:5 [待解决问题]

一下是代码:我想让这个代码生成器,在文件存在的时候不生成,为什么不行,我保存一下,他生成了所有代码文件,再保存一下居然删除了所有生成的文件,就这样一直循环

请教了 十分感谢

<#@ template language="C#" debug="True" #>
<#@ output extension="cs" #>
<#@ Assembly Name="System.Core" #>
<#@ import namespace="System.IO" #>
<#@ Import Namespace="System.Linq" #>
<#@ Import Namespace="System.Text" #>
<#@ import namespace="System.Reflection" #>
<#@ Import Namespace="System.Collections.Generic" #>
<#@ Assembly Name="$(SolutionDir)\Frame.Model\bin\Debug\Frame.Model.dll" #>
<#@ include file="T4Toolbox.tt" #>
<#@ include file="ServiceInterfaceTemplate.ttinclude" #>
<#
string currentPath = Path.GetDirectoryName(Host.TemplateFile);
string projectPath =currentPath.Substring(0, currentPath.IndexOf(@"\T4"));
string solutionPath = currentPath.Substring(0, currentPath.IndexOf(@"\Frame.Model"));
string modelFile= projectPath+@"\bin\debug\Frame.Model.dll";

byte[] fileData= File.ReadAllBytes(modelFile);
Assembly assembly = Assembly.Load(fileData);
IEnumerable<Type> modelTypes = assembly.GetTypes().Where(m =>!m.IsAbstract&&m.Namespace=="Frame.Model.EfModel");

foreach(Type modelType in modelTypes)
{
Frame.Model.T4.T4ModelInfo model = new Frame.Model.T4.T4ModelInfo(modelType, true);
//添加servce接口文件
ServiceInterfaceTemplate config = new ServiceInterfaceTemplate(model);
//必须为当前模板设置项目名称 才能输出到非当前项目路径文件夹下
var path = solutionPath+@"\Frame.Service\Interface\"+config.FileName;
if(!File.Exists(path))
{
config.Output.Project = solutionPath+@"\Frame.Service\Frame.Service.csproj";
config.Output.Encoding = Encoding.UTF8;
//config.Output.File = @"Interface\"+config.FileName;
//config.Render();
//另一种写法
config.RenderToFile(path);
}
}
#>

keeppuching的主页 keeppuching | 初学一级 | 园豆:6
提问于:2016-01-15 16:26
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册