Sergejs
Sergejs

Reputation: 2580

T4. Error: An expression block evaluated as Null

I've added template.tt file that looks like:

<#@ template language="C#" debug="true" #>
<#@ output extension=".cs" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections.Generic" #>

using System;
using System.Collections.Generic;

namespace Test
{
    public class <#= this.ClassName#>
    {       

    }
}

<#+
    public string ClassName { get; set; }
#>

I'm receiving error:

An expression block evaluated as Null
at Microsoft.VisualStudio.TextTemplating.ToStringHelper.ToStringWithCulture(Object objectToConvert)...

What should I do to avoid seeing these messages?

Thanks in advance

Upvotes: 3

Views: 2341

Answers (2)

Vaibhav Jain
Vaibhav Jain

Reputation: 34407

I assume, you want to generate something like

using System;
using System.Collections.Generic;

namespace Test
{
    public class MyClass
    {       

    }
}

The issue in the code is in expression block you are reffering to a variable <#= this.ClassName#> that doesn't exist in the class feature block. Modify the code as below.

<#@ template language="C#" debug="true" #>
<#@ output extension=".cs" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections.Generic" #>

using System;
using System.Collections.Generic;

namespace Test
{
    public class <#= this.ClassName #> //Expression Block
    {       
    }
}

<#+ //Class feature block
    public string ClassName = "MyClass";
#>

Upvotes: 3

Matt Ward
Matt Ward

Reputation: 47947

The problem is that the ClassName property is null. One way to fix the error would be to change the code in the class feature block to:

<#+
    private string className = "";

    public string ClassName {
        get { return className; }
        set { className = value; }
    }
#>

Upvotes: 4

Related Questions