I am trying to write a class in T4 template. It gives the error:
Compiling transofrmation: Type of namespace definition, or end-of-file expected
if I have the following code:
<#+
namespace Learn {
public class Converter
{
}
}
#>
But it works fine if I remove namespace
<#+
public class Converter
{
}
#>
My question is, why T4 doesn't recognize the namespace
?
<#+ #> is a class feature block. Anything you put inside this block will be written inside a class statement. When you add a namespace T4 will generate and try to compile something like this:
This is not valid C# code, namespaces can not exist within a class statement. When you do it without the namespace you will get this:
Which is valid c# code, your class will be a sub class of the one the T4 compiler creates.
Here is a link to the msdn docs that explain the supported tags. See the section "Class feature control blocks". Just remember that whatever you type into a tt or t4 file will be parsed and turned into .net code so you have to follow all the normal syntax rules.