反汇编代码中的属性顺序

发布于 2024-10-10 08:16:43 字数 596 浏览 3 评论 0原文

当检查使用 .NET Reflector 导出的反汇编代码时,我有注意到属性的顺序任何给定的实体并不总是得到维护。

我在比较同一程序集的两个版本的反汇编代码时看到了这一点(在两个版本之间看到了一些细微的代码更改),在版本之间实际上没有更改的类中。

例如

[WebBrowsable, Personalizable]
public int SomeProperty ...

vs

[Personalizable, WebBrowsable]
public int SomeProperty ...

这非常不方便,因为它看起来好像没有改变的文件已经改变了。

在这种情况下,什么控制着属性的顺序?可以采取什么措施来维持顺序?

When inspecting disassembled code that has been exported using .NET Reflector I have noticed that the order of attributes on any given entity is not always maintained.

I've seen this when comparing the disassembled code of two versions of the same assembly (that has seen some minor code changes between the two versions), in classes that have not actually changed between the versions.

e.g.

[WebBrowsable, Personalizable]
public int SomeProperty ...

vs

[Personalizable, WebBrowsable]
public int SomeProperty ...

This is pretty inconvenient as it makes it look as if files that have not changed, have.

What controls the order of attributes in this scenario, and is there anything that can be done to maintain the order?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云胡 2024-10-17 08:16:43

根据规范

在此类列表中指定属性的顺序以及附加到同一程序实体的部分的排列顺序并不重要。例如,属性规范 [A][B]、[B][A]、[A, B] 和 [B, A] 是等效的。

这并不是说没有什么办法可以维持顺序,但它确实表明编译器不必注意属性的顺序,因此可以根据需要自由地重新排序。如果您想保证顺序,您可能必须在构建后修改组件。可能有一种更简单的方法可以解决您的问题。

Per the specification

The order in which attributes are specified in such a list, and the order in which sections attached to the same program entity are arranged, is not significant. For instance, the attribute specifications [A][B], [B][A], [A, B], and [B, A] are equivalent.

That doesn't quite say that there is nothing that can be done to maintain the order, but it does say that the compiler doesn't have to pay attention to the ordering of the attributes and thus is free to reorder as it see fit. If you want to guarantee the ordering, you are probably going to have to modify the assembly post-build. There is probably a simpler way to solve your problem.

夜空下最亮的亮点 2024-10-17 08:16:43

属性的顺序不会影响代码的语义,而只是 C# 编译器如何编译项目的产物。这可能是混乱的紧急行为,具体取决于项目中的其他内容。不幸的是,你对此无能为力......

The order of attributes does not affect the semantics of the code, and is simply an artefact of how the C# compiler compiles a project. it's probably chaotic emergant behaviour depending on what else is in your project. There's not much you can do about this, unfortunately...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文