gpt4 book ai didi

.net - 防止 svcutil 生成 "EmitDefaultValue=false"属性

转载 作者:行者123 更新时间:2023-12-04 06:19:23 33 4
gpt4 key购买 nike

我最近一直在尝试从 xsd 文件生成数据契约(Contract),使用 svcutil 像这样:

svcutil.exe /t:code /dconly /out:MyContract.cs /n:*,My.Namespace MyDataDefinition.xsd

XSD 主要由如下定义组成:
<xsd:complexType name="SomeComplexObjectType">
<xsd:sequence>
<xsd:element name="FirstData" type="xsd:string" minOccurs="0" />
<xsd:element name="SecondData" type="xsd:string" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>

svcutil 生成如下内容:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="SomeComplexObjectType")]
public partial class PersonInfo : object, System.Runtime.Serialization.IExtensibleDataObject
{
private string FirstDataField;
private string SecondDataField;

[System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
public string FirstData
{
get
{
return this.FirstDataField;
}
set
{
this.FirstDataField= value;
}
}

[System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
public string SecondData
{
get
{
return this.SecondDataField;
}
set
{
this.SecondDataField= value;
}
}
}

哪个工作正常,但是,“EmitDefaultValue=false”属性不是必需的。更不用说它在 wsdl 中引入了很多噪音,添加了这样的东西:
<xsd:element minOccurs="0" name="FirstData" nillable="true" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
<DefaultValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/" EmitDefaultValue="false"/>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>

目前我只是手动编辑生成的契约(Contract),但从维护的角度来看这并不理想。

有谁知道如何防止 svcutil 自动生成这些 EmitDefaultValue=false 属性?

最佳答案

请查看 this article .

On schema import, the EmitDefaultValue property is automatically set to false whenever the WCF-specific annotation mentioned previously is detected. It is also set to false for reference types that have the nillable property set to false to support specific interoperability scenarios that commonly occur when consuming ASP.NET Web services



看来您唯一的选择是修改传入的架构,以便相关元素可以为空。

关于.net - 防止 svcutil 生成 "EmitDefaultValue=false"属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6794325/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com