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的核心代码如下:
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) #>"); } } } <#}#>
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 }
<#@ 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 }
原文地址:http://blog.csdn.net/anjingyatou/article/details/43700463