我正在运行一个自定义应用程序,它导入 WSDL 并生成 C# 源代码,使用 WSDLImporter 类来读取合同。
XSD 序列类型被转换为本机数组。为了能够生成自定义集合类型,我可以设置哪些选项?
架构:
<xs:complexType name="getAllSourcesResponse">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0"
name="return" type="tns:Source"/>
</xs:sequence>
</xs:complexType>
变成代码:
public partial class getAllSourcesResponse
{
[System.Xml.Serialization.XmlElementAttribute("return",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PaymentSource[] @return;
public getAllSourcesResponse()
{
}
public getAllSourcesResponse(Source[] @return)
{
this.@return = @return;
}
}
我查看了 SvcUtil.exe 代码,它似乎执行以下操作,但它似乎对我的应用程序生成的代码没有任何影响。
WsdlImporter importer = ...
XsdDataContractImporter contractImporter =
new XsdDataContractImporter(codeCompileUnit);
ImportOptions importOptions = new ImportOptions();
importOptions.ReferencedCollectionTypes.Add(
typeof(System.Collections.Generic.IList<>));
contractImporter.Options = importOptions;
importer.State.Add(typeof(XsdDataContractImporter), contractImporter);
@CasperOne,这个模式片段
<xs:complexType name="getClassesForPackageResponse">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
生成一个 string[] 类型:
public partial class getClassesForPackageResponse
{
public string[] @return;
public getClassesForPackageResponse() {}
public getClassesForPackageResponse(string[] @return)
{ this.@return = @return; }
}
这不会导致字符串集合使用 List:
ImportOptions importOptions = new ImportOptions();
importOptions.ReferencedCollectionTypes.Add(
typeof(System.Collections.Generic.List<string>));