简体   繁体   English

如何从WSDL在.Net中生成自定义集合类型?

[英]how to generate custom collection type in .Net from WSDL?

I am running a custom application that imports WSDLs and generates C# source code, using WSDLImporter class to read in contracts. 我正在运行一个自定义应用程序,该应用程序使用WSDLImporter类读取合同来导入WSDL并生成C#源代码。

XSD sequence types are translated into native arrays. XSD序列类型被转换为本地数组。 What options can I set in order to be able to generate custom collection types? 我可以设置哪些选项以便能够生成自定义集合类型?

Schema: 架构:

<xs:complexType name="getAllSourcesResponse">
    <xs:sequence>
        <xs:element maxOccurs="unbounded" minOccurs="0" 
            name="return" type="tns:Source"/>
    </xs:sequence>
</xs:complexType>

becomes code: 变成代码:

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;
    }
}

I looked into SvcUtil.exe code, it appears to do the following, but it does not seem to make any difference in what code my application produces. 我查看了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, this schema snippet @CasperOne,此架构片段

<xs:complexType name="getClassesForPackageResponse">
     <xs:sequence>
         <xs:element maxOccurs="unbounded" minOccurs="0" name="return" type="xs:string"/>
     </xs:sequence>
 </xs:complexType>

generates a string[] type: 生成一个string []类型:

public partial class getClassesForPackageResponse
{
    public string[] @return;
    public getClassesForPackageResponse() {}

    public getClassesForPackageResponse(string[] @return)
    {    this.@return = @return;    }
}

This does not cause string collection to use List: 这不会导致字符串收集使用列表:

ImportOptions importOptions = new ImportOptions();
importOptions.ReferencedCollectionTypes.Add(
    typeof(System.Collections.Generic.List<string>));

First, I believe you need to indicate a collection implementation, not just the interface, so that means List<T> instead of IList<T> (although you can certainly try it with the interface). 首先,我相信您需要指示一个集合实现,而不仅仅是接口,这意味着List<T>而不是IList<T> (尽管您当然可以在接口上尝试它)。

Second, you need to specify the type parameter T: 其次,您需要指定类型参数T:

ImportOptions importOptions = new ImportOptions();

importOptions.ReferencedCollectionTypes.Add(
    typeof(System.Collections.Generic.List<getAllSourcesResponse>));

As it was, you were passing the open generic type, which will not match with anything when the importer is trying to resolve the collection. 照原样,您正在传递开放的泛型类型,当导入程序试图解析集合时,该泛型类型将与任何内容都不匹配。

Just stumbled upon this while trying to figure out how to do this in Mono's WSDL importer. 刚在尝试弄清楚如何在Mono的WSDL导入器中做到这一点时偶然发现了这一点。

After reading some MSDN docs and playing around a bit with this, I got it working like this, using .NET 4.5: 在阅读了一些MSDN文档并进行了一些尝试之后,我使用.NET 4.5使其工作如下:

    var importer = new WsdlImporter (mset);
    var xsdImporter = new XsdDataContractImporter ();
    var options = new ImportOptions ();
    options.ReferencedCollectionTypes.Add (typeof (LinkedList<>));
    xsdImporter.Options = options;
    importer.State.Add (typeof (XsdDataContractImporter), xsdImporter);

You can also specify specific generic instance types such as List<int> . 您还可以指定特定的通用实例类型,例如List<int>

This is also documented here: http://msdn.microsoft.com/en-us/library/aa702680.aspx 这也记录在这里: http : //msdn.microsoft.com/en-us/library/aa702680.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM