Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4 Templating with 3rd party assemblies

Tags:

json.net

t4

I have the need to do JSON schema generation within a T4 template, and found Newtonsoft's new Schema class more than adequate for the purpose at hand (within a console application, tested), however, I cannot seem to make it play ball with the rest, as the instance to Newtonsoft always returns null.

T4 declaration:

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="Newtonsoft.Json.dll" #>
<#@ assembly name="Newtonsoft.Json.Schema.dll" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>

The assembly references point to the DLL files, and I have folder look ups set in the project settings for the project, screen shot below:

enter image description here

Trying to do something like the below, fails, because Newtonsoft cannot be found:

var schema = Newtonsoft.Json.Schema.JSchema.Parse(jsoncontent);

Error thrown is: Metadata file 'Newtonsoft.Json.Schema.dll" could not be found.

like image 750
JadedEric Avatar asked Feb 10 '23 18:02

JadedEric


1 Answers

T4 templates do not use the reference path defined in the project. T4 does support some variables inside Visual Studio:

<#@ assembly name="$(SolutionDir)\MyProject\bin\Debug\SomeLibrary.Dll" #>

There is an existing StackOverflow question about this.

If you are referencing the .dll and it is being copied into the output directory you should be able to use $(TargetDir) in the path so you do not need to include the NuGet package version number which will change when you update the NuGet package.

like image 154
Matt Ward Avatar answered Mar 27 '23 06:03

Matt Ward