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

du *_*oll 2 c# console properties class

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

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

这是我的代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

Den*_* K. 5

这样的方式:

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);
            }
}
Run Code Online (Sandbox Code Playgroud)