Search code examples
c#.nett4template-engine

T4 template: double.NaN not rendering correctly


I have a T4 template, which creates a series of static readonly variables derived from a pre-defined data set.

If I pass double.NaN as a constructor argument, the template output renders as NaN, not double.NaN, as I would have expected.

The relevant part of the template is:

<#= double.IsNaN(element.MeltingPoint) ? double.NaN : element.MeltingPoint #>

Which results in the compiler error:
The name 'NaN' does not exist in the current context

If I manually change the code to read double.NaN then it compiles just fine.

How can I force the output to read as the literal string double.NaN?


Solution

  • the result of calling double.NaN.ToString() (which the T4 Engine does in order to print the result in the generated file) is the string "NaN" (as a matter of fact, it is culture-specific).

    If you want the literal string double.NaN, then use a string literal:

    <#= double.IsNaN(element.MeltingPoint) ? "double.NaN" : element.MeltingPoint.ToString() #>