惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
Jina AI
Jina AI
H
Help Net Security
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
L
LangChain Blog
Recorded Future
Recorded Future
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
GbyAI
GbyAI
B
Blog RSS Feed
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
M
MIT News - Artificial intelligence
爱范儿
爱范儿
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
B
Blog
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
MongoDB | Blog
MongoDB | Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
The Cloudflare Blog
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
D
DataBreaches.Net
博客园 - 【当耐特】
T
Troy Hunt's Blog
V
Visual Studio Blog
V2EX - 技术
V2EX - 技术
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google Online Security Blog
Google Online Security Blog
The GitHub Blog
The GitHub Blog

博客园 - kalman

nodejs+express+jade安装备忘 Asp.net MVC应用在IIS7上部署后403错误解决方案 【转】禁止从终端服务器复制文件 FluentData Mysql分页的一个BUG 代码生成器Kalman Studio2.2发布,完美支持Oracle,不需要安装Oracle客户端 【备忘】Oracle常用系统表(做代码生成器用得到) 开发辅助工具Kalman Studio2.0发布,内置基于T4的代码生成器 如何在Yii Framework中使用PHPExcel组件【备忘】 T4代码生成器Kalman Studio发布 发布基于T4模板引擎的代码生成器[Kalman Studio] 请谨慎设置WinForm控件DataGridView列的AutoSizeMode属性 安装apache_2.0.63-win32-x86出现no installed service named "apache2" - kalman .NET序列化与反序列化(转) 手工查杀Win32/Pacex.Gen,Win32/Genetik,Win32/PSW.Agent.NCC,Win32/PSW.QQPass.VD病毒 win2003下安装Look n Stop网络防火墙导致系统蓝屏(tcpip.sys - address F75F5390 base at F75B4000,DataStamp 4473b09e) 在线播放器代码大全(收藏) - kalman - 博客园 System.Exception: System.Data.OracleClient requires Oracle client software version 8.1.7 or greater 服务器: 消息 15135,级别 16,状态 1,过程 sp_validatepropertyinputs,行 100. 对象无效。不允许在 '.cash_flux' 上使用扩展属性,或对象不存在 Excel组件使用配置文档下载
如何为Kalman Studio编写T4模板 - kalman - 博客园
kalman · 2010-05-09 · via 博客园 - kalman

  昨天刚发布了Kalman Studio代码生成器,有人说T4模板不大会编写,其实很简单,会C#语法就会编写,我这里简单的介绍一下

首先,打开Visual Studio,新建一个文本文件,将扩展名改为"tt"

这时Visual Studio会为Entity.tt生成一个对应的类文件Entity.cs,这时因为Visual Studio内置了一个代码生成工具TextTemplatingFileGenerator,会自动为每个模板文件生成对应的类文件,如果你在Entity.tt中编辑好模板,然后打开Entity.cs,TextTemplatingFileGenerator会自动解析模板,将代码输出到文件Entity.cs


我们这里不需要使用TextTemplatingFileGenerator生成代码,把属性面板,自定义工具里面的文字TextTemplatingFileGenerator删掉,对应的类文件会自动删除;然后可以开始编写模板了,我这里先展示一个编写好的实体模板,然后再详细说明

代码

<#@ template language="C#v3.5" hostSpecific="true" debug="true" #>
<#@ output extension=".cs" #>
<
    TableHost host 
= (TableHost)(Host); 
    SOTable table 
= host.Table;
    List
<SOColumn> list = host.ColumnList;
    
string columnPrefix = host.GetString("ColumnPrefix");
    
int prefixLevel = host.GetInt32("PrefixLevel");
    
string nameSpace = host.GetString("NameSpace");
    
string className = host.GetString("ClassName");
    
if(string.IsNullOrEmpty(nameSpace))nameSpace = "Entity";
    
if(string.IsNullOrEmpty(className))className = table.Name;
#
>
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;namespace <#= nameSpace #>
{
    
/// <summary>
    
/// <#= table.Comment == "" ? table.Name : table.Comment #>
    
/// </summary>
    [Serializable]
    
public partial class <#= className #>
    {
        
<foreach (SOColumn c in list)
        { #
>/// <summary>
        
/// <#= c.Comment == "" ? c.Name : c.Comment #>
        
/// </summary>
        public <#= TypeUtil.DbType2TypeString(c.DataType) #> <#= StringUtil.RemovePrefix(c.Name, columnPrefix, prefixLevel).Replace(" """) #> { getset; }
        
        
<# } #>
        
    }
}

现在我为大家简单介绍一下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用法

<#   #>  定义代码块

<#= #>  定义表达式

<#+ #>  定义变量

我觉得讲了这些大家应该可以很轻松的编写T4模板了,下次再为大家讲解如何实现一个自定义的TextTemplatingEngineHost来生成代码