【问题标题】:Ordering of properties on an object [duplicate]对象属性的排序[重复]
【发布时间】:2016-02-18 20:41:06
【问题描述】:
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
    }
}

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

【问题讨论】:

  • 请问你为什么关心?

标签: c# linq reflection


【解决方案1】:

来自MSDN

GetProperties 方法不返回特定的属性 顺序,例如字母顺序或声明顺序。您的代码不得 取决于返回属性的顺序,因为 顺序不同。

【讨论】:

    猜你喜欢
    • 2012-12-24
    • 2014-03-06
    • 2023-03-15
    • 2019-12-10
    • 1970-01-01
    • 2016-11-25
    • 2017-04-06
    • 2010-10-26
    • 2014-09-17
    相关资源
    最近更新 更多