I have a T4 C# file in which I need to reference a constant in a static class. The static class is in the same namespace.
Is this possible?
The below is merely an illustration. I need to calculate the actual constant based on existing constants, but there is also a call to an extension method involved. To keep it simple, I am merely illustrating the concept.
.cs file:
namespace me
{
public static class Stat
{
public const int Const = 1;
}
}
. tt file:
...
namespace me
{
public static int Test
{
return <#= Stat.Const #>;
}
}
It is indeed possible. However you need to do a couple things first:
<#@ assembly name="$(TargetPath)" #>
<#@ import namespace="ns" #>
That should do it. The tricky bit is in realising that your .tt doesn't work like any old class file in your project. Because it is generated it needs something to generate from, which in this case is your project dll that you have to generate beforehand.
And as promised, here is the blog post I wrote about this question :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With