反汇编代码中的属性顺序
当检查使用 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据规范
这并不是说没有什么办法可以维持顺序,但它确实表明编译器不必注意属性的顺序,因此可以根据需要自由地重新排序。如果您想保证顺序,您可能必须在构建后修改组件。可能有一种更简单的方法可以解决您的问题。
Per the specification
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.
属性的顺序不会影响代码的语义,而只是 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...