.GetProperties()
该方法在 C# 中需要多少纳秒?
编辑
测试:
在简单的开发计算机上(没什么花哨的),item 有 8 个属性,并且没有继承:
stopwatch.Start();
for (int i = 0; i < 10000; i++)
{
PropertyInfo[] properties = item.GetType().GetProperties();
}
stopwatch.Stop();
结果:
而且
当使用额外的嵌套 foreach 循环第二次运行时,总共只用了 6 毫秒。这是添加的代码(在 for 循环内):
foreach (var prop in properties)
{
var x = prop;
}