Does GetCustomAttributes() preserve the attribute order in .NET?

前端 未结 4 1408
-上瘾入骨i
-上瘾入骨i 2020-12-11 01:17

The title pretty much says it all. When I\'m doing some reflection through my classes, will the MemberInfo.GetCustomAttributes() method preserve the order of attributes on a

4条回答
  •  执笔经年
    2020-12-11 01:47

    The lexical ordering of elements in a file is absolutely not guaranteed to be persisted in anyway in the resulting CIL assemblies nor to be respected in the results returned from Reflection. This ordering is not even guaranteed to be the same over repeated calls within the same app domain!

    Note that MS have broken this ordering in other parts of reflection in the past (Noting as they did it that this actually caused some issues for some of their code), thus even if it happens to work at the moment there is nothing stopping this breaking in future or on different platforms.

    Consider changing your attribute model to allow expressing the semantic information directly rather than relying on ordering.

提交回复
热议问题