我想以编程方式创建Subject.cs文件。为此,当我在互联网上搜索时,我发现我可以使用T4文本模板。现在,我想使用Json.NET从JSON文件中获取数据,并将Root值设置为一个属性,如下所示:
namespace Library
{
class Subject
{
public static int forensic_medicine { get; set; }
public static int family_law { get; set; }
public static int constitutional_law { get; set; }
public static int obligations_law { get; set; }
}
}以下是JSON文件的内容:
{
"Subjects": [
{
"Root": "forensic_medicine",
"Name": "Forensic Medicine",
"StrID": "FMD",
"RowID": 746
},
{
"Root": "family_law",
"Name": "Family Law",
"StrID": "FML",
"RowID": 1239
},
{
"Root": "constitutional_law",
"Name": "Constitutional Law",
"StrID": "CNL",
"RowID": 996
},
{
"Root": "obligations_law",
"Name": "Obligations Law",
"StrID": "OBL",
"RowID": 1672
}
],
"is_restart": 0,
"book_path": "D:\\Colin\\_books\\",
"thesis_path": "D:\\Colin\\_thesises\\",
"full_screen": false
}我想,我必须使用SubjectList和Subject类来读取和获取JSON文件中的数据。为此,我使用了这样的类:
public class SubjectList
{
public List<Subject> Subjects { get; set; }
}
public class Subject
{
public string StrID { get; set; }
public string Name { get; set; }
public string Root { get; set; }
public int RowID { get; set; }
}最后,为了获取数据,我使用如下代码:
var json = JsonConvert.DeserializeObject<SubjectList>(File.ReadAllText("D:\\Colin\\_test\\inf.json"));在文本模板文件(Subjects.tt)中,代码如下:
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)Test\bin\Debug\Newtonsoft.Json.dll" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
namespace Library
{
public class Subject
{
<#
var json = JsonConvert.DeserializeObject<SubjectList>(File.ReadAllText("D:\\Colin\\_test\\inf.json"));
#>
}
}我在不做任何其他事情的情况下得到了以下错误。
Compiling transformation: The name 'JsonConvert' does not exist in the current context
Compiling transformation: The type or namespace name 'SubjectList' could not be found (are you missing a using directive or an assembly reference?)
我认为我必须在Subjects.tt文件中使用SubjectList和Subject类,但我不知道如何做到这一点。更重要的是,如何才能以编程方式创建Subject.cs文件而不会出现任何问题?
发布于 2019-04-15 22:53:20
特别是在您的情况下,尝试直接使用程序集名称添加引用的程序集。顺便说一句,相对于文本模板本身,读入json文件也很好。示例:
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ assembly name="Newtonsoft.Json" #>
<#@ import namespace="Newtonsoft.Json" #>
<#@ import namespace="Newtonsoft.Json.Linq" #>
<#@ output extension=".cs" #>
<#
string templateFileName = Path.GetFileNameWithoutExtension(this.Host.TemplateFile);
string settingsPath = this.Host.ResolvePath($"{templateFileName}.json");
string settingsJson = File.ReadAllText(settingsPath);
var datasource = JObject.Parse(settingsJson);
#>发布于 2018-12-20 01:28:15
至少,您需要为Newtonsoft名称空间添加一个额外的<#@ import namespace="Newtonsoft.Json" #>
https://stackoverflow.com/questions/52950990
复制相似问题