打印对象的列表<>属性值 C# [英] Print object's List<> property value C#

查看:82
本文介绍了打印对象的列表<>属性值 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象类型,例如:

I have an object type of e.g.:

Class
{
  public string Variable {get; set;}
  public List<AnotherClass> ListVariable {get; set;}
}

AnotherClass
{
  public string Variable {get; set;}
  public int IntVariable {get; set;}
}

我尝试了几种解决方案(ObjectDumperobject.GetProperties)来将所有 Class 对象值打印到屏幕上;该问题最终导致无法打印 List.而不是所有的项目,我只得到它的计数属性.

I've tried several solutions (ObjectDumper, object.GetProperties) to print all the Class object values to screen; The problem concludes in inability to print List<AnotherClass>. Instead of all it's items I get only it's count property.

尝试过的解决方案:

如何递归打印使用反射获取对象属性的值

递归获取属性 &对象的子属性

查找对象的所有属性和子属性

还有几个..

好的,正如我所见,我可能没有很好地描述问题.我需要打印对象的所有属性及其值,当我不知道对象的类型及其属性时.如果对象仅包含简单的属性,则列出的解决方案工作正常.如果属性之一是 List<>

Ok, as I see, I probably didn't describe the problem well. I need to print all the object's properties and their values, when I don't know type of the object and it's properties. The listed solutions work fine, if object contains only simple properties. The problem shows up if one of the properties is List<>

我尝试了以下方法:

1)

private static void PrintObject(Object dataSource)
{
   foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(dataSource))
   {
      string name = descriptor.Name;
      object value = descriptor.GetValue(dataSource);

      RichTextBox.Text += name + ": " + value + "\n";

      PrintObject(control, value);
   }
}

给我输出:

检查状态:已执行

TextData: System.Collections.Generic.List`1[TextDataField]

TextData: System.Collections.Generic.List`1[TextDataField]

容量:16

计数:15

但我期待这里的所有 15 个项目值,而不仅仅是列表计数.

but I was expecting all 15 item values here, not just list count.

2)

RichTextBox.Text = dataSource.DumpToString(); 

来自http://objectdumper.codeplex.com/

提供几乎相同的输出.

推荐答案

dumper"方法是最简单的实现方法,当您控制了你想看的课.这也是最安全的方法,因为您可以转储非public的内容,同时防止直接访问外部人员.

"dumper" methods are the easiest and the most correct way of doing it, when you are in control of the source of the classes you want to look at. It's also the safest method because you can dump non-public stuff while preventing direct access to outsiders.

实现这一点的最简单方法是覆盖 Object 提供给任何类型的 ToString() 方法.这样,您的代码将变得干净且可读.

Simplest way to achieve this is to override the ToString() method which Object provides to any type. This way, your code will be clean and readable.

您也可以通过 System.Reflection 来完成,但它会更复杂并且不会提供额外的好处.KISS是这里的关键词.

You might also do it through System.Reflection but it would be more complicated and provide no additional benefit. KISS is the keyword here.

像这样(在 LINQPad 中测试):注意 public 的东西应该大写,像这样:public int Variable

Like this (tested in LINQPad): NOTE public stuff is supposed to be capitalized, like this: public int Variable

void Main()
{
    Class myClassObj = new Class();
    myClassObj.Variable = 1;
    myClassObj.ListVariable = new List<AnotherClass>();
    myClassObj.ListVariable.Add(new AnotherClass{ Variable="some", IntVariable=2 });

    Console.WriteLine(myClassObj.ToString());
}

public class Class
{
    public int Variable{get;set;}
    public List<AnotherClass> ListVariable {get;set;}

    public override string ToString()
    {
        return string.Join(", ", new string[]{ 
            string.Format("Variable= {0}", + this.Variable),
            string.Format("ListVariable= [{0}]", string.Join(", ", this.ListVariable))
        });
    }
}

public class AnotherClass
{
    public string Variable{get;set;}
    public int IntVariable{get;set;}

    public override string ToString()
    {
        return string.Join(", ", new string[]{
            string.Format("Variable={0}", this.Variable),
            string.Format("IntVariable={0}", this.IntVariable)
        });
    }
}

这篇关于打印对象的列表&lt;&gt;属性值 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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