T4模板批量生成代码

一、本篇主要讲T4模板的介绍和使用T4模板动态的生成各种文件或者类文件。

1. T4模板文件的新建和介绍:

新建文件方式如下:

以下是新建的模本文件内容

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".txt" #>

 初始模板文件内容解释:

<#@ template debug="false" hostspecific="false" language="C#" #>  :指的是这个模板,不能调试,不提供host这个属性,使用的是C#语言语法
<#@ assembly name="System.Core" #>                                :assembly指引用命名空间类
<#@ import namespace="System.Linq" #>                  :类似于using system.Linq
<#@ import namespace="System.Text" #>                              :类似于using system.text
<#@ import namespace="System.Collections.Generic" #>               同上
<#@ output extension=".txt" #>                                     :指运行T4模板文件内容最终将生产.txt对应后缀的文件。如果需要生成.CS文件内容,替换便可

case1;第一个模板文件使用,内容如下,红色部分为书写内容,模本语言语句必须包含在<# #>中
复制代码
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".txt" #>

<# for(int i=0;i<10;i++){ #>
<#=i#>
<#}#>
复制代码

运行结果如下:

0
1
2
3
4
5
6
7
8
9:

 

case2:在EF框架下使用T4模板,为其动态生成类
复制代码
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
 output extension=".cs"#> 
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);

string inputFile = @"..\\Model\\Model1.edmx";          //找到model文件夹路径下,将其中内容添加到ItemCollection中

EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();

EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);

#>
using System;
using System.Collections.Generic;     //没有放在for循环中的代码会被直接打印出来
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;

namespace IDAL
{
   
<#
// Emit Entity Types

foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))  //循环获取其中的值
{
    //fileManager.StartNewFile(entity.Name + "RepositoryExt.cs");
    //BeginNamespace(namespaceName, code);    
#>    
    public partial interface I<#=entity.Name#>Dal :IBaseDal<<#=entity.Name#>>
    {
      
    }
<#}#>
    
}
复制代码

 最终结果如下:

复制代码
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;

namespace IDAL
{
   
    
    public partial interface IActionInfoDal :IBaseDal<ActionInfo>
    {
      
    }
    
    public partial interface IDepartmentDal :IBaseDal<Department>
    {
      
    }
    
    public partial interface IR_UserInfo_ActionInfoDal :IBaseDal<R_UserInfo_ActionInfo>
    {
      
    }
    
    public partial interface IRoleInfoDal :IBaseDal<RoleInfo>
    {
      
    }
    
    public partial interface IUserInfoDal :IBaseDal<UserInfo>
    {
      
    }
    
}
复制代码

最后推荐一篇相关博客:https://www.cnblogs.com/zeje/p/5248340.html 

posted @   锦大大的博客呀!  阅读(1056)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示