对象的属性排序

gh9*_*gh9 -1 c# linq reflection

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

我的问题是,如果没有OrderBy在for循环中我保证List的本机顺序将是在对象中声明属性的顺序

Bra*_*ner 5

来自MSDN:

GetProperties方法不以特定顺序返回属性,例如按字母顺序或声明顺序.您的代码不得依赖于返回属性的顺序,因为该顺序会有所不同.