c# - 生成的 C# 代码中的 T4 缩进

标签 c# t4

使用T4生成C#代码时,四处散落的TABS无法正确识别:

public partial class Foo : Base
{
        public int C { get; set; }
        [MappedProperty("A.B[{C}].Foo")]
    public int Foo { get; set; }
}

我使用的是一个看似正确缩进的 .TT 代码,类似于以下内容:

public partial class <#= ViewModelName #>
{
    <#  foreach(var property in ViewModelProperties) { #> 
        <# if(property.Mapping != null) { #>
        [MappedProperty("<#= property.Mapping #>")]
        <# } #>
        public <#= property.TypeDeclaration #> <#= property.MemberName #> { get; set; }
    <# } #>
}

此代码片段反射(reflect)了我已经尝试做的事情:尽可能将控制语句和 block 放在一行中。

最佳答案

我喜欢这样做,从来没有遇到过任何问题。

public partial class <#= ViewModelName #>
{
<#
    foreach(var property in ViewModelProperties) { 
        if(property.Mapping != null) { 
#>
    [MappedProperty("<#= property.Mapping #>")]
<#
        }
#>
    public <#= property.TypeDeclaration #> <#= property.MemberName #> { get; set; }
<#
    }
#>
}

关于c# - 生成的 C# 代码中的 T4 缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33501119/

相关文章:

c# - 在 Asp.Net Mvc 6 中创建管理员 super 用户

c# - WPF DataGrid(在 .NET 4.0 中,不是 Toolkit),使用 ICollectionView 进行 header 排序

c# - 如何处理T4字符串模板?

templates - T4 模板生成不需要的输出文件

c# - 构建时 T4 转换的产品仅在下一次构建中使用

C# 从 MDB 获取数据并将结果放入列表框中

c# - excel 2010和2013中的数据模型//数据检索

条码扫描器的 C# 编程 : RS232 vs Keyboard Wedge vs USB

templates - T4 POCO模板——删除.tt文件中自动生成的.cs文件

c# - 如何使用 .tt 模板创建文件夹?