2012-07-05 43 views
-2

當我調用這個方法時,內存會增加(1M-3M):engine.ProcessTemplate(inputTemplate,host)。我不知道爲什麼?爲什麼內存會增加(t4模板)

注:我使用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 
    } 
} 
+2

請原諒我的無知,但增加3MB的內存消耗真的很重要嗎?你能否進一步解釋你的情況? – 2012-07-05 10:13:28

+0

我同意@IlianPinzon,3MB不算什麼!我的樹莓派甚至獲得了80塊。 – leppie 2012-07-05 11:58:41

回答

0

我不知道怎麼在這裏引起你的關注,或者是什麼情況導致您需要直接實例化引擎,而不是使用VS服務接口。但是,如果引擎的實例化是第一次在給定的VS進程中調用任何T4方法,那麼NGEN的程序集將會出錯,從而導致工作集跳轉。如果它在調用ProcessTemplate的時候,那麼這可能是第一次在你的場景中使用你實際的參數組件。沒有更多的上下文有點難以分辨。