gpt4 book ai didi

c# - 对象转储类

转载 作者:IT王子 更新时间:2023-10-29 03:50:13 24 4
gpt4 key购买 nike

我正在寻找一个可以以类似于此的格式输出对象及其所有叶值的类:

User
- Name: Gordon
- Age : 60
- WorkAddress
- Street: 10 Downing Street
- Town: London
- Country: UK
- HomeAddresses[0]
...
- HomeAddresses[1]
...

(或更清晰的格式)。这相当于:

public class User
{
public string Name { get;set; }
public int Age { get;set; }
public Address WorkAddress { get;set; }
public List<Address> HomeAddresses { get;set; }
}

public class Address
{
public string Street { get;set; }
public string Town { get;set; }
public string Country { get;set; }
}

PropertyGrid 控件的一种字符串表示形式,无需为每种类型实现大量设计器。

PHP 有一个叫做 var_dump 的东西。 .我不想使用 watch ,因为它是用来打印的。

如果存在这样的东西,有人能指点我吗?或者,写一个赏金。

最佳答案

在 sgmoore 的链接中发布的对象转储器:

//Copyright (C) Microsoft Corporation.  All rights reserved.

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

// See the ReadMe.html for additional information
public class ObjectDumper {

public static void Write(object element)
{
Write(element, 0);
}

public static void Write(object element, int depth)
{
Write(element, depth, Console.Out);
}

public static void Write(object element, int depth, TextWriter log)
{
ObjectDumper dumper = new ObjectDumper(depth);
dumper.writer = log;
dumper.WriteObject(null, element);
}

TextWriter writer;
int pos;
int level;
int depth;

private ObjectDumper(int depth)
{
this.depth = depth;
}

private void Write(string s)
{
if (s != null) {
writer.Write(s);
pos += s.Length;
}
}

private void WriteIndent()
{
for (int i = 0; i < level; i++) writer.Write(" ");
}

private void WriteLine()
{
writer.WriteLine();
pos = 0;
}

private void WriteTab()
{
Write(" ");
while (pos % 8 != 0) Write(" ");
}

private void WriteObject(string prefix, object element)
{
if (element == null || element is ValueType || element is string) {
WriteIndent();
Write(prefix);
WriteValue(element);
WriteLine();
}
else {
IEnumerable enumerableElement = element as IEnumerable;
if (enumerableElement != null) {
foreach (object item in enumerableElement) {
if (item is IEnumerable && !(item is string)) {
WriteIndent();
Write(prefix);
Write("...");
WriteLine();
if (level < depth) {
level++;
WriteObject(prefix, item);
level--;
}
}
else {
WriteObject(prefix, item);
}
}
}
else {
MemberInfo[] members = element.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance);
WriteIndent();
Write(prefix);
bool propWritten = false;
foreach (MemberInfo m in members) {
FieldInfo f = m as FieldInfo;
PropertyInfo p = m as PropertyInfo;
if (f != null || p != null) {
if (propWritten) {
WriteTab();
}
else {
propWritten = true;
}
Write(m.Name);
Write("=");
Type t = f != null ? f.FieldType : p.PropertyType;
if (t.IsValueType || t == typeof(string)) {
WriteValue(f != null ? f.GetValue(element) : p.GetValue(element, null));
}
else {
if (typeof(IEnumerable).IsAssignableFrom(t)) {
Write("...");
}
else {
Write("{ }");
}
}
}
}
if (propWritten) WriteLine();
if (level < depth) {
foreach (MemberInfo m in members) {
FieldInfo f = m as FieldInfo;
PropertyInfo p = m as PropertyInfo;
if (f != null || p != null) {
Type t = f != null ? f.FieldType : p.PropertyType;
if (!(t.IsValueType || t == typeof(string))) {
object value = f != null ? f.GetValue(element) : p.GetValue(element, null);
if (value != null) {
level++;
WriteObject(m.Name + ": ", value);
level--;
}
}
}
}
}
}
}
}

private void WriteValue(object o)
{
if (o == null) {
Write("null");
}
else if (o is DateTime) {
Write(((DateTime)o).ToShortDateString());
}
else if (o is ValueType || o is string) {
Write(o.ToString());
}
else if (o is IEnumerable) {
Write("...");
}
else {
Write("{ }");
}
}
}

2015 年更新

YAML 也很好地满足了这个目的,这就是 YamlDotNet 可以做到的

安装包 YamlDotNet

    private static void DumpAsYaml(object o)
{
var stringBuilder = new StringBuilder();
var serializer = new Serializer();
serializer.Serialize(new IndentedTextWriter(new StringWriter(stringBuilder)), o);
Console.WriteLine(stringBuilder);
}

关于c# - 对象转储类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1347375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com