C#如何通過T4自動(dòng)生成代碼詳解
T4簡(jiǎn)介
T4(Text Template Transformation Toolkit)在 Visual Studio 中,“T4 文本模板”是由一些文本塊和控制邏輯組成的混合模板,它可以生成文本文件。 在 Visual C# 或 Visual Basic 中,控制邏輯編寫為程序代碼的片段。生成的文件可以是任何類型的文本,例如網(wǎng)頁(yè)、資源文件或任何語(yǔ)言的程序源代碼。
T4 文本模板有兩種類型:
1、運(yùn)行時(shí)模板
可在應(yīng)用程序中執(zhí)行運(yùn)行時(shí) T4 文本模板(“預(yù)處理過的”模板)以便生成文本字符串(通常作為其輸出的一部分)。
若要?jiǎng)?chuàng)建運(yùn)行時(shí)模板,請(qǐng)向您的項(xiàng)目中添加“已預(yù)處理的文本模板”文件。
另外,您還可以添加純文本文件并將其“自定義工具”屬性設(shè)置為“TextTemplatingFilePreprocessor”。
2、設(shè)計(jì)時(shí)模板
在 Visual Studio 中執(zhí)行設(shè)計(jì)時(shí) T4 文本模板,以便定義應(yīng)用程序的部分源代碼和其他資源。
通常,您可以使用讀取單個(gè)輸入文件或數(shù)據(jù)庫(kù)中的數(shù)據(jù)的多個(gè)模板,并生成一些 .cs、.vb 或其他源文件。
每個(gè)模板都生成一個(gè)文件。 在 Visual Studio 或 MSBuild 內(nèi)執(zhí)行它們。
若要?jiǎng)?chuàng)建設(shè)計(jì)時(shí)模板,請(qǐng)向您的項(xiàng)目中添加“文本模板”文件。 另外,您還可以添加純文本文件并將其“自定義工具”屬性設(shè)置為“TextTemplatingFileGenerator”。
通過T4模板生成代碼,運(yùn)行時(shí)實(shí)現(xiàn)
關(guān)鍵代碼段:Host
using Microsoft.VisualStudio.TextTemplating;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeGenerate.EngineHost
{
 public class TextTemplatingEngineHost : ITextTemplatingEngineHost, ITextTemplatingSessionHost
 {
 public List<string> LocalDlls { get; set; }
 public List<string> Namespaces { get; set; }
 /// <summary>
 /// 模板文件
 /// </summary>
 public string TemplateFile { get; set; }
 /// <summary>
 /// 文件擴(kuò)展名
 /// </summary>
 public string FileExtension { get; set; }
 /// <summary>
 /// 文件擴(kuò)展名
 /// </summary>
 public Encoding FileEncoding { get; set; }
 /// <summary>
 /// 錯(cuò)誤信息
 /// </summary>
 public CompilerErrorCollection Errors { get; set; }
 public IList<string> StandardAssemblyReferences
 {
  get
  {
  LocalDlls.Add(typeof(System.Uri).Assembly.Location);
  return LocalDlls;
  }
 }
 public IList<string> StandardImports
 {
  get
  {
  Namespaces.Add("System");
  return Namespaces;
  }
 }
 /// <summary>
 /// 參數(shù)傳遞
 /// </summary>
 public ITextTemplatingSession Session { get; set; }
 public bool LoadIncludeText(string requestFileName, out string content, out string location)
 {
  content = System.String.Empty;
  location = System.String.Empty;
  if (File.Exists(requestFileName))
  {
  content = File.ReadAllText(requestFileName);
  return true;
  }
  else
  {
  return false;
  }
 }
 public object GetHostOption(string optionName)
 {
  object returnObject;
  switch (optionName)
  {
  case "CacheAssemblies":
   returnObject = true;
   break;
  default:
   returnObject = null;
   break;
  }
  return returnObject;
 }
 public string ResolveAssemblyReference(string assemblyReference)
 {
  if (File.Exists(assemblyReference))
  {
  return assemblyReference;
  }
  string candidate = Path.Combine(Path.GetDirectoryName(this.TemplateFile), assemblyReference);
  if (File.Exists(candidate))
  {
  return candidate;
  }
  return "";
 }
 public Type ResolveDirectiveProcessor(string processorName)
 {
  if (string.Compare(processorName, "XYZ", StringComparison.OrdinalIgnoreCase) == 0)
  {
  //return typeof(); 
  }
  throw new Exception("Directive Processor not found");
 }
 public string ResolvePath(string fileName)
 {
  if (fileName == null)
  {
  throw new ArgumentNullException("the file name cannot be null");
  }
  if (File.Exists(fileName))
  {
  return fileName;
  }
  string candidate = Path.Combine(Path.GetDirectoryName(this.TemplateFile), fileName);
  if (File.Exists(candidate))
  {
  return candidate;
  }
  return fileName;
 }
 public string ResolveParameterValue(string directiveId, string processorName, string parameterName)
 {
  if (directiveId == null)
  {
  throw new ArgumentNullException("the directiveId cannot be null");
  }
  if (processorName == null)
  {
  throw new ArgumentNullException("the processorName cannot be null");
  }
  if (parameterName == null)
  {
  throw new ArgumentNullException("the parameterName cannot be null");
  }
  return String.Empty;
 }
 public void SetFileExtension(string extension)
 {
  FileExtension = extension;
 }
 public void SetOutputEncoding(System.Text.Encoding encoding, bool fromOutputDirective)
 {
  FileEncoding = encoding;
 }
 public void LogErrors(CompilerErrorCollection errors)
 {
  Errors = errors;
 }
 public AppDomain ProvideTemplatingAppDomain(string content)
 {
  return AppDomain.CreateDomain("Generation App Domain");
 }
 public ITextTemplatingSession CreateSession()
 {
  return this.Session;
 }
 }
}
Session
using Microsoft.VisualStudio.TextTemplating;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace CodeGenerate.EngineHost
{
 [Serializable]
 public class TextTemplatingSession : Dictionary<string, Object>, ITextTemplatingSession, ISerializable
 {
  public Guid Id { get;private set; }
  public TextTemplatingSession() : this(Guid.NewGuid())
  {
  }
  public TextTemplatingSession(Guid id)
  {
   this.Id = id;
  }
  public override int GetHashCode()
  {
   return Id.GetHashCode();
  }
  public TextTemplatingSession(SerializationInfo info, StreamingContext context)
   : base(info, context)
  {
   Id = (Guid)info.GetValue("Id", typeof(Guid));
  }
  void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  {
   base.GetObjectData(info, context);
   info.AddValue("Id", Id);
  }
  public override bool Equals(object obj)
  {
   var o = obj as TextTemplatingSession;
   return o != null && o.Equals(this);
  }
  public bool Equals(ITextTemplatingSession other)
  {
   return other != null && other.Id == this.Id;
  }
  public bool Equals(Guid other)
  {
   return other.Equals(Id);
  }
 }
}
入口
string templateFileName = "Template/test.tt";
   TextTemplatingEngineHost host = new TextTemplatingEngineHost();
   Engine engine = new Engine();
   //引入本地dll
   host.LocalDlls = new List<string>() { AppDomain.CurrentDomain.BaseDirectory.ToString() + "Params.dll" };
   //引入命名空間
   host.Namespaces = new List<string>() { "Params" };
   //模板文件
   host.TemplateFile = templateFileName;
   //設(shè)置輸出文件的編碼格式
   host.SetOutputEncoding(System.Text.Encoding.UTF8, false);
   //通過Session將參數(shù)傳遞到模板
   EngineHost.TextTemplatingSession keyValuePairs = new EngineHost.TextTemplatingSession();
   testType t = new testType() { Name = "666666666666" };
   keyValuePairs.Add("test", t);
   host.Session = keyValuePairs;
   //模板
   string input = File.ReadAllText(templateFileName);
   //執(zhí)行代碼生成
   string output = engine.ProcessTemplate(input, host);
   //設(shè)置文件的輸出路徑和文件擴(kuò)展名 ,,根據(jù)模板中的設(shè)置定義
   string outputFileName = string.Concat(
    AppDomain.CurrentDomain.BaseDirectory.ToString(), "Output/",
    Path.GetFileNameWithoutExtension(templateFileName),
    host.FileExtension);
   //將生成的文件寫入到新位置
   File.WriteAllText(outputFileName, output, host.FileEncoding);
   if (host.Errors.HasErrors)
   {
    foreach (CompilerError error in host.Errors)
    {
     MessageBox.Show(error.ToString());
    }
   }
tt文件
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ assembly name="Params.dll" #>
<#@ import namespace="Params" #>
<#@ parameter type="Params.testType" name="test" #>
<#@ output extension=".cs" #>
<# if(test!=null&&test.Name!=null){#>
<#=test.Name #>
<# } #>
自定義參數(shù)
[Serializable]
 public class testType
 {
  public string Name { get; set; }
 }
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)我們的支持。
欄 目:C#教程
下一篇:Win10下C# DateTime出現(xiàn)星期幾問題的解決方法
本文標(biāo)題:C#如何通過T4自動(dòng)生成代碼詳解
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5050.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的方法
 - 01-10Extjs4如何處理后臺(tái)json數(shù)據(jù)中日期和時(shí)間
 - 01-10C#通過Semaphore類控制線程隊(duì)列的方法
 - 01-10asp.net中XML如何做增刪改查操作
 - 01-10C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫(kù)中
 - 01-10C#通過正則表達(dá)式實(shí)現(xiàn)提取網(wǎng)頁(yè)中的圖片
 - 01-10解析C#中的常量及如何在C#編程中定義常量
 - 01-10如何使用C#從word文檔中提取圖片
 - 01-10c#檢測(cè)文本文件編碼的方法
 


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
 - 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
 - 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語(yǔ)言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
 - 01-10C#停止線程的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機(jī)閱讀
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
 - 01-10delphi制作wav文件的方法
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
 - 01-10C#中split用法實(shí)例總結(jié)
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 04-02jquery與jsp,用jquery
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 


