こんなプロジェクトを作る。
classbody.txt
namespace <#= namespacename #>
{
public class <#= classname #> {
<# for (int i = 0; i < properties.Length; i++) { #>
private <#= properties[i].Typename #> <#= properties[i].Privatename #> ;
public <#= properties[i].Typename #> <#= properties[i].Publicname #>
{
get
{
return <#= properties[i].Privatename #> ;
}
set
{
<#= properties[i].Privatename #> = value ;
}
}
<# } #>
}
}
propertydefine.txt
<#+
class PropertyDefine
{
public string Typename;
public string Privatename;
public string Publicname;
public PropertyDefine(string typename, string privatename, string publicname)
{
Typename = typename;
Privatename = privatename;
Publicname = publicname;
}
}
#>
T4Test.tt
<#@ template language="C#v3.5" debug="true" #>
<#@ output extension="cs" encoding="utf-8" #>
<#@ include file="Templates/classbody.txt" #>
<#@ include file="Templates/propertydefine.txt" #>
<#+
string namespacename = "T4Test";
string classname = "T4TestClass";
PropertyDefine[] properties = {
new PropertyDefine("string","a","A")
};
#>
すると T4Test.cs にこんなソースができる。
namespace T4Test
{
public class T4TestClass {
private string a ;
public string A
{
get
{
return a ;
}
set
{
a = value ;
}
}
}
}
classbody.txt や propertydefine.txt は使い回しが出来る。
classbody.txt を変更すれば MVVM のVM も簡単に作れる。
便利な時代になったもんだ。