使用 Json.NET 从 JSON 文件获取数据并使用 T4 文本模板创建 c-sharp 文件

2 c# t4 json json.net

我想以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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是 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
}
Run Code Online (Sandbox Code Playgroud)

我认为,我必须使用SubjectListSubject类来从 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; }
}
Run Code Online (Sandbox Code Playgroud)

最后,为了获取数据,我使用如下代码:

var json = JsonConvert.DeserializeObject<SubjectList>(File.ReadAllText("D:\\Colin\\_test\\inf.json"));
Run Code Online (Sandbox Code Playgroud)

在文本模板文件(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"));
        #>
    }
}
Run Code Online (Sandbox Code Playgroud)

我在没有做任何其他事情的情况下收到以下错误。

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?)

我想我必须使用文件中的SubjectListSubjectSubjects.tt,但我不知道该怎么做。更重要的是,如何Subject.cs以编程方式创建文件而不出现任何问题?

das*_*h88 6

要使用程序集中的任何引用(无论是 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);
#>
Run Code Online (Sandbox Code Playgroud)