zoukankan      html  css  js  c++  java
  • 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 

  • 相关阅读:
    iOS事件机制,以及不同手势使用touchesBegan等表现形式
    UIview 动画
    核心动画与UIView
    代理与Block
    关于清除浮动的几个写法
    关于一个自适应屏幕的高宽
    关于loading 的一个css样式
    用margin还是用padding?(3)—— 负margin实战
    jquery回顾part1——选择器
    解读mysql主从配置及其原理分析(Master-Slave)
  • 原文地址:https://www.cnblogs.com/wangjinya/p/10860162.html
Copyright © 2011-2022 走看看