我试图让 If else 语句根据从循环传入的对象计算值。这个模板一直有效,直到我在 if else block 中添加并且它说它不能用作语句。想法??
<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="CookieCutterDT" #>
<#@ import namespace="CookieCutterBL.DT_Template" #>
namespace <#= NameSpace #>
{
public class <#= ClassName #>
{
<#
foreach(ColumnDT c in Columns)
{#>
public <# if (c.IsNullable && c.DataType != "string" && c.DataType != "string []") { c.DataType + "?"; } else { c.DataType; } #> <#= c.ColumnName #> { get; set; };
<#
}
#>
}
}
if else 正在检查该列是否为可空字段,如果是,则使其数据类型在 C# 中也可为空。
最佳答案
if
的 then/else 部分中的表达式不会生成代码。你应该这样写:
public <# if (c.IsNullable && c.DataType != "string" && c.DataType != "string []") { #>
<#= c.DataType + "?" #>
<# } else { #>
<#= c.DataType #>
<# } #> <#= c.ColumnName #> { get; set; };
或者,使用条件运算符 ?:
一个更短的选择是:
<#= (c.IsNullable && c.DataType != "string" && c.DataType != "string []") ? (c.DataType + "?") : c.DataType #>
关于c# - T4 模板 If Else 语句写出对象而不是文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35759324/