Tech Tips

  1. Programming
  2. 1 view

How to Print an Object by Console.WriteLine in C#

I found the following FAQ (Japanese) and it says that using Microsoft.CodeAnalysis.CSharp.Scripting.

For my own reference, I’ll document how to use Microsoft.CodeAnalysis.CSharp.Scripting, as introduced in the FAQ above, in a new console project below.

Commands:

dotnet new console --use-program-main
dotnet add package Microsoft.CodeAnalysis.CSharp.Scripting --version 4.12.0
dotnet build
./bin/Debug/net8.0/FormatTest
# FormatTargetObject { Id=1, Name="John Doe" }

Codes:

namespace FormatTest;

using Microsoft.CodeAnalysis.CSharp.Scripting.Hosting;

class FormatTargetObject
{
    public int Id { get; set; } = 1;
    public string Name { get; set; } = "John Doe";
}

class Program
{
    private static readonly CSharpObjectFormatter formatter = CSharpObjectFormatter.Instance;
    static void Main(string[] args)
    {
        Console.WriteLine(formatter.FormatObject(new FormatTargetObject()));
    }
}

Programming recent post

  1. How to Print an Object by Console.WriteLine i…

  2. How to develop C# console application with co…

  3. The Benefits of PubSubClient and Points to Be…

  4. Memo of How to Build Java Develop Environment…

  5. How to Upload Program to Arduino Using Platfo…

関連記事

PAGE TOP