Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对象上属性的排序_C#_Linq_Reflection - Fatal编程技术网

C# 对象上属性的排序

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

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

GetProperties方法不返回特定类型中的属性 顺序,如字母顺序或声明顺序。您的代码不能 取决于返回属性的顺序,因为 顺序各不相同


我可以问你为什么在乎吗?
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
    }
}