T4 模板只在我“调试”它们时运行

6

我在这里遇到了一些麻烦 - 我的T4模板应该生成C#类,但当我保存或“运行自定义工具”时,它们无法成功运行。然而 - 只有当我“调试T4模板”时,它才能成功运行而没有错误。

错误如下:

Error   1   Running transformation: System.MissingMethodException: Method not found: 'System.Collections.Generic.IEnumerator`1<Newtonsoft.Json.Linq.JToken> Newtonsoft.Json.Linq.JArray.GetEnumerator()'.
   at JsonCSharpClassGenerator.JsonClassGenerator.GenerateClass(JObject[] examples, JsonType type)
   at JsonCSharpClassGenerator.JsonClassGenerator.GenerateClasses() in c:\Users\Nero\Documents\Visual Studio 2013\Projects\BluePOCO\JsonCSharpClassGenerator\JsonClassGenerator.cs:line 82
   at ApiTransformer.ClassGenerator.Generate(String json, String className) in c:\Users\Nero\Documents\Visual Studio 2013\Projects\BluePOCO\ApiTransformer\ClassGenerator.cs:line 25
   at Microsoft.VisualStudio.TextTemplatingDCEBAFBE8B1AF87B73F34AE53B7F0A1037491F7FD56EC9906754016DE7399CD0992B239FF4A836115489D16A9EE78F6DCBA10BE4137758F32395F1DB7ADF7FF1.GeneratedTextTransformation.TransformText() C:\Users\Nero\documents\visual studio 2013\Projects\BluePOCO\BluePOCO\BlueprintTransformer.tt   1   1   BluePOCO

很明显,这与Json.Net有关。我认为问题可能在于Json.Net包中,也许是版本之间的差异 - 但解决方案中的所有项目都使用完全相同的版本。
JsonClassGenerator是这个文件: http://jsonclassgenerator.codeplex.com/SourceControl/latest#JsonCSharpClassGeneratorLib/JsonClassGenerator.cs 第82行是:
GenerateClass(examples, rootType);

明显的解决方案是……调试。然而,当我在调试时无法调试某些东西,因为它在调试时不会出错。我似乎已经用尽了我的头脑,有什么想法可以尝试吗?当然,模板运作正常,但每次我想通过右键单击-> 调试来更新它们时都需要运行它们吗?似乎并没有按照预期工作。
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Windows.Forms" #>
<#@ assembly name="$(SolutionDir)\BluePOCO\bin\Debug\ApiTransformer.dll" #>
<#@ assembly name="$(SolutionDir)BluePOCO\bin\Debug\RestSharp.dll"#>
<#@ import namespace="ApiTransformer"#>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Net"#>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="RestSharp"#>
<#@ output extension=".cs" #>
<#
var filename = "Source.txt";
var filepath = Path.Combine(Path.GetDirectoryName(this.Host.ResolvePath("")), "BluePOCO", filename);
var list = Transformer.GetMethodList(File.ReadAllText(filepath));
string str;
#>
using System.ComponentModel;
using Restcoration;
using RestSharp;
using Newtonsoft.Json; 
namespace BluePOCO 
{   <#foreach(var item in list){#>
<# str = item.Resource.Length > 1 ? string.Join("", item.Resource.Split('/').Select(x => x.Length > 0 ? x.Substring(0, 1).ToUpper() + x.Substring(1) : "")) : "Root";#>

<#
    var methodList = new List<string>(); 
    foreach(var response in item.Responses)
    {
        methodList.Add(string.Format("{0} = typeof({1})", Enum.GetName(typeof (HttpStatusCode), int.Parse(response.Code)), str + response.Code));
    }
    string add;  
    if(methodList.Any())
        add = ", " + string.Join(", ", methodList);
    else
        add = "";
#>

[Rest(Method = Method.<#=item.Method#><#=add#>)]<#foreach(var response in item.Responses){#>

<#=ClassGenerator.Generate(response.Json, str+response.Code)#>

<#}#>
<#}#>

}

你解决了这个问题吗?我五年后也遇到了同样的情况。 - rball
2个回答

0

尝试

<#@ template debug="true"


0

当我的项目使用旧版的Newtonsoft.Json.dll v4.5.11.15520时,我遇到了同样的问题。

在切换到最新版本的Newtonsoft.Json.dll v8.0.3.19514后,问题消失了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接