T4模板读取某个类的所有字段

这个文章的初衷只是方便自己,为的是以后可以快速找个备份。

注:用SortedList是为了自动排序。
在工作经常碰到A对象与B对象相似度90%然后就可以用下面的代码稍稍改进一下便可省去不少事,根因手笨经常出错,所以能复制复制,能不用手的全不用手。
如果有更好的办法,欢迎留言.

Love is real when shared!


<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Reflection" #>
<#@ assembly name="D:\testDll\Test.MyMode.dll" #>
 

			  <# 			  
			  var typ = GetProperties();
            foreach(var p in typ)
            {
			  #><#=p.Value#> 
			  <#}
            #>
 
<#+
	public SortedList <string,string> GetProperties(){
		var res=new SortedList <string,string>();
		foreach (PropertyInfo propertyInfo in typeof(Test.MyMode.WareInfoEntity).GetProperties())
				{
				res.Add(propertyInfo.Name,propertyInfo.Name);
				} 
		return res;
	}
#>

  

 

posted on 2015-10-10 15:19  肖恩__劉  阅读(370)  评论(0)    收藏  举报

导航