T4使用第三方程序集进行模板化

时间:2015-06-24 06:52:51

标签: json.net t4

我需要在T4模板中生成JSON模式,并且发现Newtonsoft的新Schema类超出了手头的目的(在控制台应用程序中,经过测试),但是,我似乎无法让它与其余部分一起玩,因为Newtonsoft的实例总是返回null。

T4声明:

import sys
sys.path.insert(0, 'shared')

import pkg

程序集引用指向DLL文件,我在项目的项目设置中设置了文件夹查找,屏幕截图如下:

enter image description here

尝试执行类似下面的操作失败,因为无法找到Newtonsoft:

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

引发的错误是:元数据文件' Newtonsoft.Json.Schema.dll"无法找到

2 个答案:

答案 0 :(得分:6)

T4模板不使用项目中定义的参考路径。 T4确实支持Visual Studio中的一些变量:

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

existing StackOverflow question这个。

如果您正在引用.dll并将其复制到输出目录中,您应该能够在路径中使用 $(TargetDir),这样您就不需要包含NuGet包版本更新NuGet包时将更改的数字。

答案 1 :(得分:1)

Found the solution to this was not as specific as the original error stated.

My Newtonsoft.Json version is version 7.0.1 but the compiled version of Newtonsoft.Json.Schema was against version 6.0.8, which caused an internal "version difference" error, but never got raised to the top of the stack, and T4 just notified that the metadata could not be found (theoretically correct) but not very specific.

I grabbed a copy of Newtonsoft.Json.Schema from GitHub, and compiled that against version 7, from NuGet and the error went away.