码迷,mamicode.com
首页 > Web开发 > 详细

.Net T4 模板 实例(一)

时间:2015-02-10 11:15:28      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:c#   wpf   mvvm   t4   

T4模板基础就不在这里重复了,园子里有很多文章讲解T4模板。

今天给大家介绍一个实例 主要是针对WPF MVVM中Model的书写,

Model的格式大致是:

    // 测试
    private string a;

    /// <summary>
    /// 测试
    /// <summary>
    public string A
    {
        get 
        { 
            return a;
        }
        set
        {
            if (a != value)
            {
                a = value;
                this.RaisePropertyChanged("A");
            }
        }
    }

在Model 类中一般都是重复性质的工作,所有选择T4来生成该文件是个不错的选择。

生成model的核心代码如下:

BaseModelT4.tt

<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="MVVMT4.models" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ include file="BaseT4.tt" #>
<#foreach(ModelT4Model item in ModelT4OC) 
{
#>

    // <#=item.Notes#>
    private <#=item.TypeName#> <#=Lower(item.PropertyName) #>;

    /// <summary>
    /// <#=item.Notes#>
    /// <summary>
    public <#=item.TypeName #> <#=Uppercase(item.PropertyName) #>
    {
        get 
        { 
            return <#=Lower(item.PropertyName) #>;
        }
        set
        {
            if (<#=Lower(item.PropertyName) #> != value)
            {
                <#=Lower(item.PropertyName) #> = value;
                this.RaisePropertyChanged("<#=Uppercase(item.PropertyName) #>");
            }
        }
    }
<#}#>

BaseModelT4_P.cs

public partial class BaseModelT4
    {
        #region 字段

        private ObservableCollection<ModelT4Model> _ModelT4OC = new ObservableCollection<ModelT4Model>();

        #endregion

        #region 属性

        public ObservableCollection<ModelT4Model> ModelT4OC
        {
            get { return _ModelT4OC; }
            set { _ModelT4OC = value; }
        }

        #endregion
    }

BaseT4.tt

<#@ 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" #>
<#+public string Uppercase(string s)
    {
        if(string.IsNullOrEmpty(s))
        {
            return string.Empty;
        }

        string notConvertS = s.Substring(1);
        string convertS=s.Substring(0,1);
        convertS = convertS.ToUpperInvariant();
        return convertS + notConvertS;
    }
    public string Lower(string s)
    {
        if(string.IsNullOrEmpty(s))
        {
            return string.Empty;
        }

        string notConvertS = s.Substring(1);
        string convertS=s.Substring(0,1);
        convertS = convertS.ToLowerInvariant();
        return convertS + notConvertS;
    }
#>
ModelT4Model.cs

public class ModelT4Model : INotifyPropertyChanged
    {
        #region 字段

        private string _TypeName = string.Empty;
        private string _PropertyName = string.Empty;
        //注释
        private string _Notes = string.Empty;

        #endregion

        #region 属性

        public string TypeName
        {
            get { return _TypeName; }
            set
            {
                if (_TypeName != value)
                {
                    _TypeName = value;
                    OnPropertyChanged("TypeName");
                }
            }
        }

        public string PropertyName
        {
            get { return _PropertyName; }
            set
            {
                if (_PropertyName != value)
                {
                    _PropertyName = value;
                    OnPropertyChanged("PropertyName");
                }
            }
        }

        /// <summary>
        /// 注释
        /// </summary>
        public string Notes
        {
            get { return _Notes; }
            set
            {
                if (_Notes != value)
                {
                    _Notes = value;
                    OnPropertyChanged("Notes");
                }
            }
        }

        #endregion

        #region 方法

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region 事件

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }


源码下载

.Net T4 模板 实例(一)

标签:c#   wpf   mvvm   t4   

原文地址:http://blog.csdn.net/anjingyatou/article/details/43700463

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!