更新时间:2023-02-06 22:13:09
节点顺序在 XML 中显然很重要,如下所示:
Node order obviously matters in XML like this:
<p>
<span>This wouldn't make much sense</span>
<span>if the order of these nodes were reversed.</span>
</p>
它在 XML 中不太明显,就像您提供的那样,它似乎是某种序列化格式.但是,如果属性设置顺序不正确,属性设置器有副作用的对象可能会失败.
It's less obvious in XML like what you provided, which appears to be some kind of serialization format. But objects whose property setters have side effects can fail if the properties aren't set in the right order.
想象一个具有私有 Person
字段的类,该字段公开 PersonID
和 Name
属性.PersonID
setter 创建 Person
的私有实例,Name
setter 在私有 Name
属性上设置 Name
代码>人员代码>字段.在这种情况下,在设置 PersonID
之前设置 Name
失败,因为 Person
尚不存在.
Imagine a class with a private Person
field that exposes PersonID
and Name
properties. The PersonID
setter creates the private instance of Person
, and the Name
setter sets the Name
property on the private Person
field. In this case, setting Name
before you set PersonID
fails, because Person
doesn't exist yet.
在这种情况下,实现要求 PersonID
出现在 XML 中的 Name
之前的架构可以防止此错误发生,代价是迫使其他开发人员这样做看似荒谬的事情.
This is a case where implementing a schema that requires PersonID
to appear before Name
in the XML keeps this error from happening, at the cost of forcing other developers to do things that are apparently nonsensical.
在这种情况下,显而易见的事情是找到编写该类的开发人员并击败他.这几乎是不可能的,尽管思考它曾经存在的世界很有趣.
Tthe obvious thing to do in situations like this is to find the developer who wrote that class and beat him. This is rarely possible, though it's interesting to contemplate a world in which it were.