简体   繁体   English

如何在 C# 交互式控制台中模拟 object 的打印?

[英]How to simulate the printing of object like in C# Interactive Console?

An example:一个例子:

在此处输入图像描述

If we type scoreDir in the C# Console, its content is pretty printed, ie the definition of the object, its length and its data.如果我们在 C# 控制台中键入scoreDir ,它的内容会打印得很漂亮,即 object 的定义、它的长度和它的数据。

I would like to simulate this printing with a built-in .NET method , eg ToString() .我想用内置的 .NET 方法模拟这种打印,例如ToString() However, ToString() doesn't work, as showed.但是,如图所示, ToString()不起作用。 "Simulate" means I can generate the same printed string, but store it in a variable. “模拟”意味着我可以生成相同的打印字符串,但将其存储在变量中。 Microsoft must have used some function to print such object;微软一定用过一些function来打印这样的object; it's best to just re-use the function (no re-inventing the wheels).最好只重复使用 function(不要重新发明轮子)。

There is no such build tool for this but you can convert it to Json.没有这样的构建工具,但您可以将其转换为 Json。 The problem with your example that is not so easy for a ConcurrentDictionary.您的示例的问题对于 ConcurrentDictionary 来说并不那么容易。

IMPORTANT NOTE:重要的提示:
This only will work for Newtonsoft and .NET 6.0 since System.Text.Json does not provide a cast for serialize to JSON ConcurrentDictionary and this also don't work for Newtonsoft in .NET Framework This only will work for Newtonsoft and .NET 6.0 since System.Text.Json does not provide a cast for serialize to JSON ConcurrentDictionary and this also don't work for Newtonsoft in .NET Framework

How to Serialize to JSON string a ConcurrentDictionary?如何序列化为 JSON 字符串 ConcurrentDictionary?

Import Newtonsoft.Json from Nugget从 Nugget 导入Newtonsoft.Json

Add using Newtonsoft.Json; using Newtonsoft.Json; to the references in the head of the file到文件头部的引用

     ConcurrentDictionary<(int,int), (double,int)> scoreDir = new ConcurrentDictionary<(int,int), (double,int)>();
    scoreDir.TryAdd((1,2),(0.9,3));
     string jsonString = JsonConvert.SerializeObject(scoreDir);
    Console.WriteLine(jsonString);

Returns:回报:

{"(1, 2)":{"Item1":0.9,"Item2":3}} {"(1, 2)":{"Item1":0.9,"Item2":3}}

Fiddle: https://dotnetfiddle.net/XzHHoN小提琴: https://dotnetfiddle.net/XzHHoN

Credits to @ Hans Passant in the comment section.在评论部分感谢@Hans Passant

In Visual Studio, after installing the Nuget package Microsoft.CodeAnalysis.CSharp.Scripting , the static function Microsoft.CodeAnalysis.CSharp.Scripting.Hosting.CSharpObjectFormatter.Instance.FormatObject() can be used to mimic the same output as C# Interactive Console. In Visual Studio, after installing the Nuget package Microsoft.CodeAnalysis.CSharp.Scripting , the static function Microsoft.CodeAnalysis.CSharp.Scripting.Hosting.CSharpObjectFormatter.Instance.FormatObject() can be used to mimic the same output as C# Interactive Console.

Here is the code that works:这是有效的代码:

using System;
using System.IO;
using System.Collections.Concurrent;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var scoreDir = new ConcurrentDictionary<(int, int), (double, int)>();
            scoreDir.TryAdd((1, 2), (0.9, 3));
            var res = Microsoft.CodeAnalysis.CSharp.Scripting.Hosting.CSharpObjectFormatter.Instance.FormatObject(scoreDir);
            Console.WriteLine(res);
        }
    }
}

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM