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

Whi*_*awk 7 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; }
}
Run Code Online (Sandbox Code Playgroud)

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

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

此代码段反映了我已经尝试过的操作:尽可能地将控制语句和块设置为单行.

Ant*_*hot 10

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

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


Shl*_*omo 6

使用PushIndent(),PopIndent()并ClearIndent()像这样:

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

或者...

public partial class <#= ViewModelName #>
{
<# 
   PushIndent("   "); 
   foreach(var property in ViewModelProperties) {
      if(property.Mapping != null) { 
         WriteLine("[MappedProperty({0})]", property.Mapping);
      }
      WriteLine("public {0} {1} {{ get; set; }}", property.TypeDeclaration, property.MemberName);
   }
   PopIndent(); 
#>
}
Run Code Online (Sandbox Code Playgroud)