zoukankan      html  css  js  c++  java
  • 初学T4模板

    1、T4模板语法

    <#@ template language="C#v3.5" hostSpecific="true" debug="true" #>
    这里可以指定模板使用的语言,hostSpecific="true"表示是否使用特定的host(Kalman Studio里面使用的是TableHost对象,必须实现接口ITextTemplatingEngineHost)

    <#@ output extension=".cs" #>  指定生成文件的扩展名

    <#@ assembly name="System.Data" #>
    添加程序集引用,如果要使用第三方程序集,那么最好在项目中添加引用,或者加入到GAC

    <#@ import namespace="System.Data" #>
    导入要使用的命名空间,注意:这里的命名空间必须要在前面指定的程序集里面找得到的,比如我指定命名空间"System.Data","System.Data.Common",这些在程序集System.Data中都有的

    <#@ include file="test.tt" #> 导入模板,类似Html的include用法

    <#   #>  定义代码块

    <#= #>  定义表达式

    <#+ #>  定义变量

     
    2、生成T4模板Demo
     
    <#@ 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 = @"..\T4Demo.Model\DataModel.edmx" ;//指定EF模型与当前T4模板的相对路径,从而找到相关的实体类
     
    EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
    string namespaceName = code.VsNamespaceSuggestion();
     
    EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create( this);
     
    #>
    using T4Demo.Model;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace T4Demo.IDAL
    <#
    foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))//循环遍历获取相关的实体类
    #>           
         //动态生成的模板
                     public partial interface I<#= entity.Name#>Dal : IBaseDal< <#=entity.Name#> >
        {
          
        }  
    <#} #>             
    }
    初学T4模板,有不对的还请指正。。。
  • 相关阅读:
    家庭网关
    linux -jdk 安装
    linux 常见命令--系统信息部分
    pyglet--EventLoop对象(主事件循环,用于从系统消息队列中取出消息,并派发给各个窗口)
    ATL com的dll文件与tlb文件
    MFC实现COM组件
    如何定义一个接口(接口Interface只在COM组件中定义了,MFC和C++都没有接口的概念)
    关于DLL调试的两个工具(dependency walker和dumpbin.exe)
    MFC工程名称与所包含文件名称的关系(工程名可以更改,输出的.dll.exe.lib都以最后工程名命名为准)
    关于c++中命名空间namespace
  • 原文地址:https://www.cnblogs.com/zcz527/p/3376960.html
Copyright © 2011-2022 走看看