我使用XSDObjectGen.exe工具从 xsd 文件中自动生成了一些类。我的类包含额外的公共变量,使用前导下划线命名,我不知道为什么。
这是来自 xsd 文件的示例:
<xs:attribute name="Fare" type="xs:int" use="required">
<xs:annotation>
<xs:documentation>The fare price in pence</xs:documentation>
</xs:annotation>
</xs:attribute>
相应的自动生成的 C# 代码是:
[XmlAttribute(AttributeName = "Fare", DataType = "int")]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public int __Fare;
[XmlIgnore]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public bool __FareSpecified;
[XmlIgnore]
public int Fare
{
get { return __Fare; }
set { __Fare = value; __FareSpecified = true; }
}
我了解所有这些代码,包括属性。但是,我不明白为什么以这种方式实施。
- 为什么这个类序列化
__Fare
而不是Fare
属性?在这种情况下,__Fare
变量将是私有的(并重命名_fare
)或者可以使用自动属性。 __FareSpecified
变量的目的是什么?
我们的感觉是__
-prefixed 变量只会给使用这些类的任何开发人员增加不便,因此计划重写如下:
[XmlAttribute(AttributeName = "Fare", DataType = "int")]
public int Fare{ get; set;}
甚至只是:
[XmlAttribute]
public int Fare{ get; set;}
任何人都可以阐明__
-prefixed 变量背后的基本原理吗?
请注意,我们的 xsd 文件预计不会经常更改(如果有的话),因此我们重新自动生成这些类的能力并不重要。
编辑
我在这里与团队仔细检查过,这个源代码实际上是使用 XSDObjectGen.exe 生成的,而不是我最初所说的 xsd.exe。