XSDObjectGen.exe 自动生成的代码中出现意外的 __ 前缀公共变量
我已经使用 XSDObjectGen.exe 工具。 我的类包含额外的公共变量,使用前导下划线命名,我不明白为什么。
以下是 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
变量的用途是什么?
我们的感觉是,以 __
为前缀的变量只会给使用这些类的任何开发人员带来不便,因此计划重写如下:
[XmlAttribute(AttributeName = "Fare", DataType = "int")]
public int Fare{ get; set;}
或者甚至只是:
[XmlAttribute]
public int Fare{ get; set;}
任何人都可以阐明背后的基本原理吗?以 __
为前缀的变量?
请注意,我们的 xsd 文件预计不会经常更改(如果有的话),因此我们重新自动生成这些类的能力并不重要。
编辑
我与这里的团队进行了仔细检查,该源代码实际上是使用 XSDObjectGen.exe 生成的,而不是我最初所说的 xsd.exe。
I have auto generated some classes from an xsd file using the XSDObjectGen.exe tool. My classes contain extra public variables, named using leading underscores, and I cannot figure out why.
Here's a sample from the xsd file:
<xs:attribute name="Fare" type="xs:int" use="required">
<xs:annotation>
<xs:documentation>The fare price in pence</xs:documentation>
</xs:annotation>
</xs:attribute>
The corresponding auto generated C# code is:
[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; }
}
I understand all of this code, including the attributes. However, I do not understand why it has been implemented this way.
- Why does this class serialize the
__Fare
rather than theFare
property? In this case the__Fare
variable would be private (and renamed_fare
) or an auto-property could be used. - What is the purpose of the
__FareSpecified
variable?
Our feeling is that the __
-prefixed variables are just going to add inconvenience for any developers who consume these classes, so plan to rewrite as follows:
[XmlAttribute(AttributeName = "Fare", DataType = "int")]
public int Fare{ get; set;}
Or even just:
[XmlAttribute]
public int Fare{ get; set;}
Can anyone shed any light on the rationale behind the __
-prefixed variables?
Note that our xsd file is not expected to change often, if ever, so our ability to re-auto generate these classes is not important.
Edit
I double-checked with the team here, this source code was actually generated using XSDObjectGen.exe, not xsd.exe as I originally said.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它是为了区分公共财产及其支持变量。
XSD 对不可空类型执行 XXXSpecified 的废话。
由于诸如此类的事情,我并不是 XSD 工具的特别粉丝。 查看 XSDObjectGen 或 WSDL.EXE 和看看它们是否更适合您。
Its to differentiate between the public property and its backing variable.
XSD does the XXXSpecified nonsense for non-nullable types.
I'm not a particular fan of the XSD tool because of stuff like this. Take a look at the XSDObjectGen or WSDL.EXE and see if they work better for you.