使用 T4 文本模板生成设计时代码
Visual Studio 2012
在 Visual Studio 中创建设计时 T4 模板
|
![]() |
---|
|
class MyGeneratedClass { private int P1 = 0; private int P2 = 0; private int P3 = 0; }
<#@ assembly name="System.Xml.dll" #> <#@ import namespace="System.Xml" #> <#@ import namespace="System.IO" #>
<# var properties = File.ReadLines("C:\\propertyList.txt");#> ... <# foreach (string propertyName in properties) { #> ...
<#@ template debug="false" hostspecific="true" language="C#" #>
<# string fileName = this.Host.ResolvePath("filename.txt"); string [] properties = File.ReadLines(filename); #> ... <# foreach (string propertyName in properties { #> ...
<#@ template hostspecific="true" language="C#" #> <#@ output extension=".txt" #> <#@ assembly name="EnvDTE" #> <# IServiceProvider serviceProvider = (IServiceProvider)this.Host; EnvDTE.DTE dte = (EnvDTE.DTE) serviceProvider.GetCOMService(typeof(EnvDTE.DTE)); #> Number of projects in this VS solution: <#= dte.Solution.Projects.Count #>
![]() |
---|
|
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v11.0\TextTemplating\Microsoft.TextTemplating.targets" /> <PropertyGroup> <TransformOnBuild>true</TransformOnBuild> <!-- Other properties can be inserted here --> </PropertyGroup>
自定义工具 = |
TextTemplatingFileGenerator |
生成操作 = |
无 |
<#@ template debug="false" hostspecific="false" language="C#" #> <#@ output extension=".cs" #>
|
来自: ThinkTank_引擎 > 《T4》