【问题标题】:Unexpected __-prefixed public variables in XSDObjectGen.exe auto generated codeXSDObjectGen.exe 自动生成的代码中意外的 __ 前缀公共变量
【发布时间】: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


【解决方案1】:

它用来区分公共属性和它的支持变量。

XSD 对不可为空的类型执行 XXXSpecified 废话。

因为这样的事情,我不是 XSD 工具的特别粉丝。看看XSDObjectGenWSDL.EXE 看看它们是否更适合你。

【讨论】:

    猜你喜欢
    • 2021-05-14
    • 2021-07-01
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多