[英]Wrong indentation with t4 templates
我目前正在使用T4模板,我注意到有時代碼沒有正確縮進,我怎么能避免這種情況?
例如,我在模板中有這個代碼
}
<# } #>
this.cmbDecisionList.Dat = dataSource;
this.btnDec.Enabled = dataSource.Count > 0;
}
在生成的類中它就像
}
this.cmbDecisionList.Dat = dataSource;
this.btnDec.Enabled = dataSource.Count > 0;
}
請允許我通過用點替換空格來說明您的問題。
}
....<# } #>
....this.cmbDecisionList.Dat = dataSource;
this.btnDec.Enabled = dataSource.Count > 0;
}
在生成的類中它就像
}
........this.cmbDecisionList.Dat = dataSource;
this.btnDec.Enabled = dataSource.Count > 0;
}
現在,讓我們刪除前面的點。
}
<# } #>
....this.cmbDecisionList.Dat = dataSource;
this.btnDec.Enabled = dataSource.Count > 0;
}
在生成的類中它就像
}
....this.cmbDecisionList.Dat = dataSource;
this.btnDec.Enabled = dataSource.Count > 0;
}
我認為你努力獲得可讀的生成代碼是件好事。 我們將坐下來嘗試偶爾調試生成的代碼,這樣如果它很容易就會很好(我們永遠不會編輯生成的代碼)。
我采用了一種模式,我可能會犧牲模板的一些可讀性來獲得生成的代碼可讀性。
Generated code
<#
T4 statements
#>
Generated code
IE#>總是出現在換行符之后,並且在之后立即添加換行符。
然后您的代碼將更改為:
}
<#
}
#>
this.cmbDecisionList.Dat = dataSource;
this.btnDec.Enabled = dataSource.Count > 0;
}
這樣生成的代碼往往按預期格式化。
它可能不是保留格式的唯一方法,但它是我使用的格式。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.