Help with T4 (runtime text generation in c#)?
-
So I'm trying to turn a piece of data into a simple text file in C#. From the look of it, a runtime T4 template would be perfect for the job. But I can't get it to actually accept my data types.
The template
<#@ assembly name="System.Core" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="DungeonRandomizer" #> <#@ output extension=".txt" #> <html> <head> <title>Auto-generated Adventure Locations</title> </head> <body> <h1>Generated Adventures in <#= Region #></h1> <# foreach (AdventureData d in adventures) { #> <table style="border:1px solid black"> <tr><td style="font-weight: bold">Level: <#= d.Level #></td></tr> <tr><td> Type: <#= d.AdventureType #> </td><td>Subtype: <#= d.Subtype #></td></tr> <tr><td> Scale: <#= d.Scale #></td><td>Areas: <#= d.Size #></td</tr> <tr><td> Main Monsters: <#= d.PrimaryMonster #></td><td>Boss Monster?: <#= d.HasBoss #></td></tr> </table> <br> <# } #> </body> </html> <#+ public List<AdventureData> adventures; public string Region; #>
errors out on save (which builds the template) with
Compiling transformation: The type or namespace name 'DungeonRandomizer' could not be found (are you missing a using directive or an assembly reference?)
despite having the import statement right there.I've tried adding a partial class:
namespace DungeonRandomizer { public partial class AdventureOutput { private List<AdventureData> data; private string region; public AdventureOutput(List<AdventureData> adventures) { data = adventures; region = data[0].Region; } } }
to no effect.
The documentation is less than helpful.
-
@benjamin-hall Fixed it...turns out .NET Core is missing one of the dlls used by the t4 system and it has to be added via nuget.
-
@benjamin-hall said in Help with T4 (runtime text generation in c#)?:
@benjamin-hall Fixed it...turns out .NET Core is missing one of the dlls used by the t4 system and it has to be added via nuget.
Spent about 10 minutes looking at the code in the first post...... it never said .NET Core -- that would have been really handy to know up front as this is a well known [at least to people who use T4 with Core] consideration....
-
@thecpuwizard ah. Sorry. Explains why I was so puzzled--everything looked right, but it wasn't working. And the documentation is pure crap.
-
@benjamin-hall said in Help with T4 (runtime text generation in c#)?:
And the documentation is pure crap.
Yes it is. T4 is a mess on many different levels.
-
@masonwheeler said in Help with T4 (runtime text generation in c#)?:
Yes it is. T4 is a mess on many different levels.
No argument....but still there are a number of things that you can do with T4 that I have not found other (better) ways of accomplishing.
Unfortunately, topics related to T4 do not get significant up-votes on UserVoice, so this is unlikely to change.
-
@thecpuwizard said in Help with T4 (runtime text generation in c#)?:
@masonwheeler said in Help with T4 (runtime text generation in c#)?:
Yes it is. T4 is a mess on many different levels.
No argument....but still there are a number of things that you can do with T4 that I have not found other (better) ways of accomplishing.
Unfortunately, topics related to T4 do not get significant up-votes on UserVoice, so this is unlikely to change.
It appears to be very powerful. And horribly ill defined.
-
Wait, why are you using T4 to generate HTML?
Isn't that, like, what Razor is for? And what .NET Core is for?
-
@magus This is for a desktop (console) app where the format of the page is fixed (basically a series of fixed-shape tables that get data stuffed into them)--bringing in the whole Razor engine for something like this seemed a little heavyweight.
-
@benjamin-hall To be fair, .NET Core has moved a lot of the .NET Standard Library out to NuGet.
-
@powerlord It would have been nice to have a note on the documentation: "If you're doing this in .NET Core, pull the <package> from NuGet!"
-
@benjamin-hall Docs probably haven't been updated since before .NET Core came out.
It'd be better if they just turned it into a .NET Standard 1.6 / 2.0 library as iirc you can specify which libraries .NET Core needs to pull from NuGet that you don't for .NET Framework because they're shipped with Framework.
-
@powerlord said in Help with T4 (runtime text generation in c#)?:
Docs probably haven't been updated since before .NET Core came out.
Make sure you are using content from "docs.microsoft.com" and not any of the legacy sites. This content is usually pretty much up to date, and there is a feedback mechanism for getting improvements. Other sites, not so much...
-
@Benjamin-Hall Hello, please, what's the name of Missing Nuget, I can't find it. I have the same problem. Thank you
-
@Jonyl said in Help with T4 (runtime text generation in c#)?:
@Benjamin-Hall Hello, please, what's the name of Missing Nuget, I can't find it. I have the same problem. Thank you
It's been a while, but IIRC it's the
System.CodeDom
package.