如何在字符串中自动显示类的所有属性及其值? [重复]

时间:2022-10-30 10:42:11

This question already has an answer here:

这个问题在这里已有答案:

Imagine a class with many public properties. For some reason, it is impossible to refactor this class into smaller subclasses.

想象一下有很多公共财产的班级。出于某种原因,不可能将此类重构为较小的子类。

I'd like to add a ToString override that returns something along the lines of:

我想添加一个ToString覆盖,返回以下内容:

Property 1: Value of property 1\n
Property 2: Value of property 2\n
...

Is there a way to do this?

有没有办法做到这一点?

5 个解决方案

#1


65  

I think you can use a little reflection here. Take a look at Type.GetProperties().

我想你可以在这里使用一点反思。看看Type.GetProperties()。

private PropertyInfo[] _PropertyInfos = null;

public override string ToString()
{
    if(_PropertyInfos == null)
        _PropertyInfos = this.GetType().GetProperties();

    var sb = new StringBuilder();

    foreach (var info in _PropertyInfos)
    {
        var value = info.GetValue(this, null) ?? "(null)";
        sb.AppendLine(info.Name + ": " + value.ToString());
    }

    return sb.ToString();
}

#2


20  

@Oliver's answer as an extension method (which I think suits it well)

@Oliver作为扩展方法的答案(我认为很适合)

public static string PropertyList(this object obj)
{
  var props = obj.GetType().GetProperties();
  var sb = new StringBuilder();
  foreach (var p in props)
  {
    sb.AppendLine(p.Name + ": " + p.GetValue(obj, null));
  }
  return sb.ToString();
}

#3


3  

You can do this via reflection.

你可以通过反思来做到这一点。

PropertyInfo[] properties = MyClass.GetType().GetProperties();
foreach(PropertyInfo prop in properties)
{
...
}

#4


1  

If you have access to the code of the class you need then you can just override ToString() method. If not then you can use Reflections to read information from the Type object:

如果您可以访问所需类的代码,那么您可以覆盖ToString()方法。如果没有,那么您可以使用Reflections从Type对象中读取信息:

typeof(YourClass).GetProperties()

#5


1  

You can take inspiration from a more elaborate introspection of state from the StatePrinter package class introspector

您可以从StatePrinter包类的introspector中更精细地反省状态中获取灵感

#1


65  

I think you can use a little reflection here. Take a look at Type.GetProperties().

我想你可以在这里使用一点反思。看看Type.GetProperties()。

private PropertyInfo[] _PropertyInfos = null;

public override string ToString()
{
    if(_PropertyInfos == null)
        _PropertyInfos = this.GetType().GetProperties();

    var sb = new StringBuilder();

    foreach (var info in _PropertyInfos)
    {
        var value = info.GetValue(this, null) ?? "(null)";
        sb.AppendLine(info.Name + ": " + value.ToString());
    }

    return sb.ToString();
}

#2


20  

@Oliver's answer as an extension method (which I think suits it well)

@Oliver作为扩展方法的答案(我认为很适合)

public static string PropertyList(this object obj)
{
  var props = obj.GetType().GetProperties();
  var sb = new StringBuilder();
  foreach (var p in props)
  {
    sb.AppendLine(p.Name + ": " + p.GetValue(obj, null));
  }
  return sb.ToString();
}

#3


3  

You can do this via reflection.

你可以通过反思来做到这一点。

PropertyInfo[] properties = MyClass.GetType().GetProperties();
foreach(PropertyInfo prop in properties)
{
...
}

#4


1  

If you have access to the code of the class you need then you can just override ToString() method. If not then you can use Reflections to read information from the Type object:

如果您可以访问所需类的代码,那么您可以覆盖ToString()方法。如果没有,那么您可以使用Reflections从Type对象中读取信息:

typeof(YourClass).GetProperties()

#5


1  

You can take inspiration from a more elaborate introspection of state from the StatePrinter package class introspector

您可以从StatePrinter包类的introspector中更精细地反省状态中获取灵感