Create C# classes programmatically with QuickType

773 Views Asked by At

I'm trying to do (maybe) something not possible today with QuickType. I would like to create c# classes directly in a function. In another words I want to call quicktype functions to create c# classes from json but programmatically and not use https://app.quicktype.io/ Any idea how to perform that ? Thanks in advance for your help!

1

There are 1 best solutions below

0
RyuzakiH On BEST ANSWER
  1. You need to install quicktype (Node.js must be installed)
    npm install -g quicktype
  2. Run this command in C#
    quicktype petstore.json -o petstore.cs

using this code (based on 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();