c# - 使用 Json.NET 从 JSON 文件中获取数据并使用 T4 文本模板创建 c-sharp 文件
问题描述
我想以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?)
我想我必须在文件中使用SubjectList
和Subject
类Subjects.tt
,但我不知道我是怎么做的。更重要的是,如何以Subject.cs
编程方式创建文件而不会出现任何问题?
解决方案
若要使用程序集中的任何引用(无论是像 Newtonsoft.Json 这样的 NuGet 包还是项目输出),您必须添加导入和程序集指令。具体而言,尝试通过直接使用程序集名称来添加引用的程序集。附带说明一下,阅读相对于文本模板本身的 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);
#>
推荐阅读
- html - 为什么访问的链接会禁用 svg 图标的填充属性的转换?
- reactjs - NextJS 中的 createPortal
- git - Git远程获取然后结帐时会发生大量文件更改
- python - 删除 Python 列表中的前导/尾随停用词
- xml - 在类型值上创建 XPath 过滤?
- javascript - 通过 Babel Loader 运行 Babel 时的行为不一致
- python - Python中是否有一种快速的方法可以按列和行分组来拆分?
- c# - 重新抛出 JsonConverter 异常的正确方法
- google-chrome-extension - 如何让我的扩展模拟鼠标中键点击
- git - 将“git status”信息添加到标准 zsh 配置文件