【问题标题】:Create C# classes programmatically with QuickType使用 QuickType 以编程方式创建 C# 类
【发布时间】:2020-03-02 19:41:40
【问题描述】:

我正在尝试做(也许)今天用 QuickType 做不到的事情。我想直接在函数中创建 c# 类。换句话说,我想调用 quicktype 函数来从 json 创建 c# 类,但以编程方式而不是使用 https://app.quicktype.io/ 知道如何执行吗?提前感谢您的帮助!

【问题讨论】:

  • 这能回答你的问题吗? Deserialize JSON to anonymous object
  • 在右上角 3 点的 quicktype 上应该有命令行和 GitHub。您可以 fork 或调用命令行。但是不需要快速输入,因为 Visual Studio 已经有一个特殊的过去按钮,c# Lib blogs.msdn.microsoft.com/vsx/2016/04/21/… 的某处应该有一个函数命令
  • 感谢您的回复。但是我需要动态创建关于下载的 json 的 c# 类。当我按下按钮时,所有这些都必须在后台运行。
  • 我看不出我的评论没有涵盖您评论的第一部分。对于第二个,我不明白它是如何相关的。您可以在 C# 中调用类似 stackoverflow.com/questions/1469764/run-command-prompt-commands 的执行命令。或者使用GitHub源码,直接调用truescrypt函数。
  • 两者都将提供类定义。但它不会是一个已编译并在执行项目中准备就绪的类。 stackoverflow.com/questions/3862226/…。 .此时,您可能只是回到 Cid 的第一条评论并回到动态类型。

标签: c# quicktype


【解决方案1】:
  1. 需要安装quicktype(必须安装Node.js
    npm install -g quicktype
  2. 在 C# 中运行此命令
    quicktype petstore.json -o petstore.cs

使用此代码(基于run-command-prompt-commands):

string jsonPath = @"petstore.json"; // path of the JSON file
string outputPath = @"petstore.cs"; // path of the output csharp file

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
    WindowStyle = ProcessWindowStyle.Hidden,
    FileName = "cmd.exe",
    Arguments = $"/C quicktype {jsonPath} -o {outputPath}"
};
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    相关资源
    最近更新 更多