我想知道如何在控制台上打印类属性?
我找到了一个相关主题并运行了代码。它应该可以工作,但它不起作用。
这是我的代码:
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/