'Mono.TextTemplating.Roslyn T4 pass parameter

I would like to pass a value/ parameter to a .tt file. In this case, it's the 'ClassName'. To generate the class, the Mono.TextTemplating.Roslyn NuGet package is used and .NET 6.

As soon as the .tt is executed, the next error is displayed:

System.ArgumentNullException: Value cannot be null. (Parameter 'objectToConvert')

Both 'Add' functions don't result in the expected behavior:

session.Add("ClassName", "ABC");
generator.AddParameter(null, null, "ClassName", "ABC");

What is the correct way to pass a value and use it within the .tt.

.tt file

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Xml.dll" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Xml" #>
<#@ output extension=".cs" #>
//----------------------------------------------------
//
// THIS IS AN AUTO GENERATED FILE.
//
//----------------------------------------------------

using System;

namespace ConsoleApp1
{
    public class <#= ClassName #>
    {
            
    }
}
<#+
public string ClassName { get; set; }
#>

tt execution:

string inputFile = @"C:\MyClass2.tt";
string outputFile = @"C:\ClassA2.cs";
var generator = new Mono.TextTemplating.TemplateGenerator();
var session = generator.GetOrCreateSession();

session.Add("ClassName", "ABC"); // does NOT work    
generator.AddParameter(null, null, "ClassName", "ABC"); // does NOT work

generator.ProcessTemplate(inputFile, outputFile); 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source