c# - 如何在控制台打印类属性?

标签 c# class properties console

我想知道如何在控制台上打印类属性?

我找到了一个相关主题并运行了代码。它应该可以工作,但它不起作用。

这是我的代码:

public class Function //simple example
{
    public double [] Addition(double[] parameter1, double[] parameter2)
     {
          return 0;
     }
}

System.ComponentModel

foreach(PropertyDescriptor descriptor in TypeDescriptor.GetProperties(Function))
{
    string name=descriptor.Name;
    object value=descriptor.GetValue(Function);
    Console.WriteLine("{0}={1}",name,value);
}

最佳答案

这样的方式:

public static string PrintPropreties(object obj)
{
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj))
            {
                string name = descriptor.Name;
                object value = descriptor.GetValue(obj);
                Console.WriteLine("{0}={1}", name, value);
            }
}

关于c# - 如何在控制台打印类属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31770508/

相关文章:

Python 3.x - 在将项目附加到列表之前运行函数

cocoa - 为什么在 iOS 中不需要 ivars 而在 Cocoa 中需要它们?

python - 如何识别属性的属性何时被设置?

c# - 从命名值的 IEnumerable<T> 设置实例的属性值

c# - Winforms,如何以相同的形式分隔用户控件上的选项卡索引?

c++ - 当有多个构造函数时如何设置默认构造函数

javascript - 反墨迹

svn - 获取 svn 用户名作为 maven 属性

c# - 从函数返回值 - 错误

c# - 枚举作为类属性