首页 > 代码库 > T4模板批量生成代码

T4模板批量生成代码

大家通过比对下,就应该知道怎么玩.

T4代码

<#@ template debug="false" hostspecific="true" language="C#" #>  //告诉T4引擎控制块用c#编写<#@ include file="EF.Utility.CS.ttinclude"#>                     //引入EF模板<#@ output extension=".cs" #>                                    //生成文件的后辍名<#MetadataLoader loader = new MetadataLoader(this);CodeGenerationTools code = new CodeGenerationTools(this);string inputFile = @"..\Icdce.UserLimit.Entity\WebOA.edmx";      //Edmx的相对路径EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);string namespaceName = code.VsNamespaceSuggestion();             //获取当前文件的命名空间#>using System;using System.Collections.Generic;using System.Linq;using System.Text;using Icdce.UserLimit.Entity;using Icdce.UserLimit.IDAL;namespace <#=namespaceName#>{    <# foreach(EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e=>e.Name)) { #>    public partial class <#=entity.Name#>Repository : BaseRepository<<#=entity.Name#>>, I<#=entity.Name#>Repository    {            }    <#}#>}

生成后的代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Icdce.UserLimit.Entity;using Icdce.UserLimit.IDAL;namespace Icdce.UserLimit.DAL{        public partial class RoleRepository : BaseRepository<Role>, IRoleRepository        {                }        public partial class UserInfoRepository : BaseRepository<UserInfo>, IUserInfoRepository        {                }}

 

T4模板批量生成代码