c#对象转字符串以文本格式显示 [英] c# object to string to display it in text format

查看:65
本文介绍了c#对象转字符串以文本格式显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个对象/列表ListaDatos",我喜欢将它作为清晰的字符串(以可视化/通过邮件等方式发送)

I have the next object/list "ListaDatos" and I like to get it as clear string (to visualize/send this via mail, etc)

public List<Datos> ListaDatos = new List<Datos>();

public class Datos
{
    public string Numero;
    public string Alias;
    public string URLConsumo;

    //-- Consumos ----------------------------- 
    public List<Consumo> Consumos = new List<Consumo>();
    public string ConsumoTotal;
}

public class Consumo 
{
    public string Tipo;
    public string Subtipo;
    public string Concepto;
    public string Cantidad;
    public string Importe;
    public string Total;
}

将这个对象渲染"成文本以获得具有如下内容的字符串变量的最简单方法是什么:

What is the easiest way to "render" this object into text to obtain a string variable with something like this:

DATOS
 Numero     : 10
 Alias      : "aaaaa"
 urlConsumo : "www.aaaaaaaaaaaaa"
 Consumos
  Tipo      : "abc"
  SubTtipo  : "aaa"
  ...
DATOS
 Numero     : 10
 Alias      : "aaaaa"
 urlConsumo : "www.aaaaaaaaaaaaa"
 Consumos
  Tipo      : "abc"
  SubTtipo  : "aaa"
  ...

推荐答案

Consumo 类实现一个 ToString() 方法,然后实现一个 ToString() 用于 Datos 类的方法,使用 Consumo 类的 ToString() 方法.

Implement a ToString() method for the Consumo class and then implement a ToString() method for the Datos class using the ToString() method for the Consumo class.

这篇关于c#对象转字符串以文本格式显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆