【发布时间】:2010-10-18 12:48:24
【问题描述】:
我已经使用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;}
谁能解释__-前缀变量背后的基本原理?
请注意,我们的 xsd 文件预计不会经常更改(如果有的话),因此我们重新自动生成这些类的能力并不重要。
编辑
我在这里和团队仔细核实过,这个源代码实际上是使用 XSDObjectGen.exe 生成的,而不是我最初说的 xsd.exe。
【问题讨论】:
-
我还没用过XOG。我不高兴它也扯出这种废话。为 WCF 和相关技术(例如序列化程序)创建的较新的 XML 工具比旧的 2.0 工具(恕我直言)工作得更好。
标签: c# xsd code-generation xsd.exe