C# 对象上属性的排序
我的问题是,如果for循环中没有OrderBy,我是否保证列表的本机顺序将是对象中声明属性的顺序 GetProperties方法不返回特定类型中的属性 顺序,如字母顺序或声明顺序。您的代码不能 取决于返回属性的顺序,因为 顺序各不相同C# 对象上属性的排序,c#,linq,reflection,C#,Linq,Reflection,我的问题是,如果for循环中没有OrderBy,我是否保证列表的本机顺序将是对象中声明属性的顺序 GetProperties方法不返回特定类型中的属性 顺序,如字母顺序或声明顺序。您的代码不能 取决于返回属性的顺序,因为 顺序各不相同 我可以问你为什么在乎吗? private string DoStuff(object result, StringBuilder returnBuilder) { // get a list of all public properties and ord
我可以问你为什么在乎吗?
private string DoStuff(object result, StringBuilder returnBuilder)
{
// get a list of all public properties and order by name
// so we can guarentee that the order of the headers and
// the results are the same
foreach (var prop in result.GetType().GetProperties().OrderBy(x => x.Name))
{
// do something cool
}
}