a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

C#如何通過T4自動生成代碼詳解

2019-10-29 19:58:28
字體:
來源:轉載
供稿:網友

T4簡介

T4(Text Template Transformation Toolkit)在 Visual Studio 中,“T4 文本模板”是由一些文本塊和控制邏輯組成的混合模板,它可以生成文本文件。 在 Visual C# 或 Visual Basic 中,控制邏輯編寫為程序代碼的片段。生成的文件可以是任何類型的文本,例如網頁、資源文件或任何語言的程序源代碼。

T4 文本模板有兩種類型:

1、運行時模板

可在應用程序中執行運行時 T4 文本模板(“預處理過的”模板)以便生成文本字符串(通常作為其輸出的一部分)。

若要創建運行時模板,請向您的項目中添加“已預處理的文本模板”文件。

另外,您還可以添加純文本文件并將其“自定義工具”屬性設置為“TextTemplatingFilePreprocessor”。

2、設計時模板

在 Visual Studio 中執行設計時 T4 文本模板,以便定義應用程序的部分源代碼和其他資源。

通常,您可以使用讀取單個輸入文件或數據庫中的數據的多個模板,并生成一些 .cs、.vb 或其他源文件。

每個模板都生成一個文件。 在 Visual Studio 或 MSBuild 內執行它們。

若要創建設計時模板,請向您的項目中添加“文本模板”文件。 另外,您還可以添加純文本文件并將其“自定義工具”屬性設置為“TextTemplatingFileGenerator”。

通過T4模板生成代碼,運行時實現

關鍵代碼段: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> /// 文件擴展名 /// </summary> public string FileExtension { get; set; } /// <summary> /// 文件擴展名 /// </summary> public Encoding FileEncoding { get; set; } /// <summary> /// 錯誤信息 /// </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> /// 參數傳遞 /// </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;   //設置輸出文件的編碼格式   host.SetOutputEncoding(System.Text.Encoding.UTF8, false);   //通過Session將參數傳遞到模板   EngineHost.TextTemplatingSession keyValuePairs = new EngineHost.TextTemplatingSession();   testType t = new testType() { Name = "666666666666" };   keyValuePairs.Add("test", t);   host.Session = keyValuePairs;   //模板   string input = File.ReadAllText(templateFileName);   //執行代碼生成   string output = engine.ProcessTemplate(input, host);   //設置文件的輸出路徑和文件擴展名 ,,根據模板中的設置定義   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 #><# } #>

自定義參數

[Serializable] public class testType {  public string Name { get; set; } }

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 99久久久国产精品美女 | 亚洲一区视频在线 | 中文字幕在线第二页 | 久久久久久久免费 | 亚洲成人免费观看 | 三区在线 | wwwjizz日本 | 麻豆久久 | 99精品视频在线免费观看 | 国产精品一品二区三区的使用体验 | 久久一区二区三区四区 | 国产精品久久 | 国产在线一区二区三区四区 | 美女福利视频网站 | 日本一区二区三区免费观看 | 久久久免费精品视频 | 日韩国产一区二区三区 | 午夜免费高清视频 | 欧美国产视频 | 久久av一区二区三区 | 日本色站 | 久久亚洲国产精品 | 夜夜夜久久 | 日本成人中文字幕在线观看 | 亚洲国产精品人人爽夜夜爽 | 91在线一区二区 | 欧美一区永久视频免费观看 | 狠狠视频 | 亚洲电影一区 | 久久久com| 国产亚洲网站 | 日韩一二三四 | 成人亚洲精品久久久久软件 | 久久成人综合 | 黄色网址 | 欧美一二区 | 国产精品无码永久免费888 | 精品久久网站 | 国产极品美女高潮无套av个 | 午夜免费福利视频 | 免费在线一区二区 |