Reputation: 273
I have a c# application which generates embedded C code using T4 templates. However I have a small problem with the resulting output which I wonder if there is an elegant solution.
The issue is I loop around both for loops and foreach to create enums and other tables using the following T4 template:
typedef enum eSDef_Index
{
<# for (int i = 0; i < ScreenDefinitions.Count(); i++)
{ #>
SD_IDX_<#=ScreenDefinitions[i].Name.ToUpper() #> = <#=i#>,
<# }
// >>>> Can we do a backspace here?
#>
}eSDEF_INDEX;
This successfully creates the enum i want. However the C compiler I am using does not like the last enum entry to have the comma. I could fix this by checking if this is the last item in the list, however i wonder if there is a more elgant way?
My idea was to effectively delete the last comma after it has been generate using a backspace maybe.
So is it possible to add special characters which directly affect the generated code? or any other way of doing this?
Upvotes: 0
Views: 135
Reputation: 14231
I think it's impossible.
Why not use string.Join
method?
typedef enum eSDef_Index
{
<#= string.Join(",\r\n ",
ScreenDefinitions.Select((x, i) =>
$"SD_IDX_{x.Name.ToUpper()} = {i}")
) #>
}eSDEF_INDEX;
Place comment into first parameter (separator):
<#= string.Join(", // Comment \r\n ",
ScreenDefinitions.Select((x, i) =>
"SD_IDX_" + x.Name.ToUpper() + " = " + i)
) #>
Upvotes: 1