2013-11-24 38 views
4

我在这里遇到了一些麻烦 - 我应该生成C#类的T4模板在保存或“运行自定义工具”时拒绝成功运行。但是 - 这只有当我“调试T4模板”,它运行成功没有错误。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); 

的obious的解决办法是...调试。但是,我无法调试那些...以及在调试时拒绝错误。我似乎已经精疲力尽了我的大脑,我有什么想法可以尝试?当然,模板工作正常,但每次我想通过右键单击更新来运行它们 - >调试?似乎没有按预期工作。

编辑:

<#@ 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)#> 

<#}#> 
<#}#> 

} 

回答

0

尝试

<#@模板调试= “真”

+0

可悲没有帮助:( – NeroS

0

我有同样的问题时,我的项目使用旧Newtonsoft.Json.dll V4 .5.11.15520。

我更换使用最新版本的Newtonsoft.Json.dll后,问题就消失了。 v8.0.3.19514