如何在控制台上打印类属性

本文关键字:打印 属性 控制台 | 更新日期: 2023-09-27 18:05:51

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

我找到了一个相关的主题,我运行了代码。它应该是工作的,但它没有工作。

下面是我的代码:
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);
            }
}