えムナウ Blog

えムナウ の なすがまま

目次

Blog 利用状況

ニュース


follow mnow at http://twitter.com


えムナウのプログラミングのページ

INETAJ

書庫

日記カテゴリ

ギャラリ

クラスを T4 で作る

こんなプロジェクトを作る。

project

 

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 も簡単に作れる。

便利な時代になったもんだ。

投稿日時 : 2010年2月24日 16:28

コメントを追加

No comments posted yet.
タイトル
名前
URL
コメント