起初我使用的是:
sortedList = unsorted.AsParallel().OrderBy(myItem => TypeDescriptor.GetProperties(myItem)[firstSort.Item2].GetValue(myItem));
其中,firstSort.Item2 是属性的字符串名称。然而,随着未排序列表中项目数量的增加,性能显着下降。 (正如我所料)
有没有办法在不使用反射的情况下做到这一点?
蛮力方法是执行以下操作:
if(firstSort.Item2 == "Size")
sortedList = unsorted.AsParallel().OrderBy(myItem => myItem.Size);
else if(firstSort.Item2 == "Price")
sortedList = unsorted.AsParallel().OrderBy(myItem => myItem.Price);
...
我正在寻找可以完成上述行为的东西,但不必在界面中的所有不同属性中进行硬编码。
最佳答案
您使用的所有不涉及实际属性的硬编码列表的内容都将在“幕后”使用反射。
关于c# - 如何在不使用反射的情况下通过名称动态访问属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5044255/