当我调用此方法时,内存将增加(1M-3M):engine.ProcessTemplate(inputTemplate,host)。我不知道为什么?
注意:我使用 t4 模板生成代码。
这是我的代码:
Engine engine = new Engine();
host.Session = new TextTemplatingSession();
Parameter nameSpaceParameter = new Parameter() { Text = "NameSpace", Value = this.txtNameSpaceRoot.Text };//+ strTmp.Replace("Templates." + this.CurSelectedNode.DisplayName, string.Empty)
host.Session.Add("NameSpace", nameSpaceParameter);
Parameter tableNameParameter = new Parameter() { Text = "TableName", Value = oName.Substring(2)};
host.Session.Add("TableName", tableNameParameter);
string inputTemplate = File.ReadAllText(host.TemplateFileValue);
string content=engine.ProcessTemplate(inputTemplate, host);
这是我的 t4 模板文件:
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#@ parameter name="NameSpace" type="SmartCodeGenerator.Parameter" #>
<#@ parameter name="TableName" type="SmartCodeGenerator.Parameter" #>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using <#=NameSpace.Value#>.DAL;
using <#=NameSpace.Value#>.Model;
namespace <#=NameSpace.Value#>.BLL
{
public class <#=TableName.Value#>Repository
{
RepositoryBase<<#=TableName.Value#>Model> repository = null;
public <#=TableName.Value#>Repository()
{
repository = new RepositoryBase<<#=TableName.Value#>Model>();
}
#region IRepository<T> 成员
public <#=TableName.Value#>Model Create()
{
return repository.Create();
}
public <#=TableName.Value#>Model Update(<#=TableName.Value#>Model entity)
{
return repository.Update(entity);
}
public <#=TableName.Value#>Model Insert(<#=TableName.Value#>Model entity)
{
return repository.Insert(entity);
}
public void Delete(<#=TableName.Value#>Model entity)
{
repository.Delete(entity);
}
public IList<<#=TableName.Value#>Model> FindAll()
{
return repository.FindAll();
}
public List<<#=TableName.Value#>Model> QueryByPage<TKey>(Expression<Func<<#=TableName.Value#>Model, bool>> filter, Expression<Func<<#=TableName.Value#>Model, TKey>> orderby, int OrderType, int Take, int Skip, out int recordsCount)
{
recordsCount = repository.Query(filter).Count();
if (OrderType == 0)
{
return repository.Query(filter).OrderBy(orderby).Take(Take).Skip(Skip).ToList();
}
else
{
return repository.Query(filter).OrderByDescending(orderby).Take(Take).Skip(Skip).ToList();
}
}
#endregion
}
}