C# 为什么可以';我不能把这个对象作为参数传递吗?

C# 为什么可以';我不能把这个对象作为参数传递吗?,c#,web-services,function,proxy-classes,C#,Web Services,Function,Proxy Classes,背景: 使用.NET4.0在C#中编写使用web服务的客户端。代理类包括一个record类型的对象,该对象由web服务的SOAP响应填充 情况: 该API包括两个函数,findRecords和getRecords。这两个函数都下载实际记录,区别在于findRecords的类型为void并通过out参数提供记录,而getRecords的类型为record,因此返回记录 问题: 执行对findRecords的调用后,我可以访问记录对象的成员(如recordID,recordTitle等),以便在其他

背景:

使用.NET4.0在C#中编写使用web服务的客户端。代理类包括一个record类型的对象,该对象由web服务的SOAP响应填充

情况:

该API包括两个函数,
findRecords
getRecords
。这两个函数都下载实际记录,区别在于
findRecords
的类型为void并通过out参数提供记录,而
getRecords
的类型为record,因此返回记录

问题:

执行对
findRecords
的调用后,我可以访问记录对象的成员(如
recordID
recordTitle
等),以便在其他函数参数中使用。但是,如果尝试将记录对象本身作为参数传递,则会得到
ArgumentNullException
。当前唯一可以将记录作为参数传递的方法是使用从
getRecords
函数返回的记录。这种方法的缺点是,它使我需要进行的API调用数量增加了一倍,这会降低我的客户端和web服务的速度

问题:

为什么它会这样,我可以做些什么来将记录对象作为参数从
findRecords
传递过来

代码:

这是findRecords函数的定义:

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://localhost:8080/findRecords", RequestNamespace="http://www.<redacted>.com/ws/schemas", ResponseNamespace="http://www.<redacted>.com/ws/schemas", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public void findRecords([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] string username, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] filter filter, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer")] string offset, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer")] string count, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] out System.Nullable<int> numResults, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] [System.Xml.Serialization.XmlIgnoreAttribute()] out bool numResultsSpecified, [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] [System.Xml.Serialization.XmlArrayItemAttribute("list", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)] out record[] results) {
        object[] results1 = this.Invoke("findRecords", new object[] {
                    username,
                    filter,
                    offset,
                    count});
        numResults = ((System.Nullable<int>)(results1[0]));
        numResultsSpecified = ((bool)(results1[1]));
        results = ((record[])(results1[2]));
    }
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://localhost:8080/getRecords", RequestNamespace="http://www.<redacted>.com/ws/schemas", ResponseNamespace="http://www.<redacted>.com/ws/schemas", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    [return: System.Xml.Serialization.XmlArrayAttribute("records", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    [return: System.Xml.Serialization.XmlArrayItemAttribute("list", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
    public record[] getRecords([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] string username, [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] [System.Xml.Serialization.XmlArrayItemAttribute("list", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer", IsNullable=false)] string[] stiIds) {
        object[] results = this.Invoke("getRecords", new object[] {
                    username,
                    recordIds});
        return ((record[])(results[0]));
    }
[System.Web.Services.Protocols.SoapDocumentMethodAttribute(“http://localhost:8080/findRecords,RequestNamespace=http://www..com/ws/schemas,ResponseNamespace=http://www..com/ws/schemas,Use=System.Web.Services.Description.SoapBindingUse.Literal,ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public void findRecords([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable=true)]字符串用户名,[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable=true)]筛选器,[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,DataType=“integer”)]字符串偏移量,[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,DataType=“integer”)]字符串计数,[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable=true)]out System.Nullable numResults[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable=true)][System.Xml.Serialization.XmlIgnoreAttribute()]out bool numResults指定的[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable=true)][System.Xml.Serialization.XmlArrayItemAttribute(“list”,Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable=false)]输出记录[]结果){
object[]results1=this.Invoke(“findRecords”,新对象[]{
用户名,
过滤器,
抵消,
计数});
numResults=((System.Nullable)(results1[0]);
numResultsSpecified=((bool)(结果1[1]);
结果=((记录[])(结果1[2]);
}
getRecords函数的定义:

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://localhost:8080/findRecords", RequestNamespace="http://www.<redacted>.com/ws/schemas", ResponseNamespace="http://www.<redacted>.com/ws/schemas", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public void findRecords([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] string username, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] filter filter, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer")] string offset, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer")] string count, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] out System.Nullable<int> numResults, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] [System.Xml.Serialization.XmlIgnoreAttribute()] out bool numResultsSpecified, [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] [System.Xml.Serialization.XmlArrayItemAttribute("list", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)] out record[] results) {
        object[] results1 = this.Invoke("findRecords", new object[] {
                    username,
                    filter,
                    offset,
                    count});
        numResults = ((System.Nullable<int>)(results1[0]));
        numResultsSpecified = ((bool)(results1[1]));
        results = ((record[])(results1[2]));
    }
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://localhost:8080/getRecords", RequestNamespace="http://www.<redacted>.com/ws/schemas", ResponseNamespace="http://www.<redacted>.com/ws/schemas", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    [return: System.Xml.Serialization.XmlArrayAttribute("records", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    [return: System.Xml.Serialization.XmlArrayItemAttribute("list", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
    public record[] getRecords([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] string username, [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] [System.Xml.Serialization.XmlArrayItemAttribute("list", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer", IsNullable=false)] string[] stiIds) {
        object[] results = this.Invoke("getRecords", new object[] {
                    username,
                    recordIds});
        return ((record[])(results[0]));
    }
[System.Web.Services.Protocols.SoapDocumentMethodAttribute(“http://localhost:8080/getRecords,RequestNamespace=http://www..com/ws/schemas,ResponseNamespace=http://www..com/ws/schemas,Use=System.Web.Services.Description.SoapBindingUse.Literal,ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[返回:System.Xml.Serialization.XmlArrayAttribute(“记录”,Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable=true)]
[返回:System.Xml.Serialization.XmlArrayItemAttribute(“list”,Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable=false)]
公共记录[]getRecords([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable=true)]字符串用户名,[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable=true)][System.Xml.Serialization.XmlArrayItemAttribute(“列表”,Form=System.Xml.Schema.XmlSchemaForm.Unqualified,DataType=“integer”,IsNullable=false)]字符串[]stiid){
object[]results=this.Invoke(“getRecords”,新对象[]{
用户名,
记录ID});
返回((记录[])(结果[0]);
}
我想用它做什么:

        // Objects to use for the findRecords call
        int? numResults;
        bool numResultsSpecified;
        record[] records;

        // Object for handling and saving the XML
        XRecord r;

                try
                {
                    ws.findRecords(usernames[uname_Index], GetFilter(), offset.ToString(), count.ToString(),
                        out numResults, out numResultsSpecified, out returnRecords);

                    for (int i = 0; i < returnRecords.Length; i++)
                    {
                        count--;
                        r = GrabRecord(returnRecords[i]);
                        r.record.Save(@".\Records\" + r.id + "_" + r.date2 + ".xml");
                    }
                }
//用于findRecords调用的对象
int?numResults;
指定的布尔数结果;
记录[]项记录;
//对象来处理和保存XML
xr;
尝试
{
ws.findRecords(用户名[uname_Index]、GetFilter()、offset.ToString()、count.ToString(),
out numResults、out numResults specified、out return records);
for(int i=0;i

    private static XRecord GrabRecord(record _record)
    {
        XNamespace nameSpace = "http://www.<redacted>.com/ws/schemas";

        XDocument r =
            new XDocument(
                new XElement(nameSpace + "getRecordsResponse",
                    new XAttribute(XNamespace.Xmlns + "ns1", nameSpace),
                    new XElement("list",
                        new XElement("ID", _record.id),
                        new XElement("title", _record.title),
            ...............
                        new XElement("comments", _record.comments),
                        new XElement("category", _record.category),
                        _record.modifiedDateSpecified ? new XElement("modifiedDate", _record.modifiedDate) : null,
                        new XElement("attachments", from a in _record.attachments
                                                    select new XElement("list",
                                                        new XElement("id", a.id),
                                                        new XElement("filePath", a.filePath),
                                                        new XElement("type", a.type))));

        XRecord xr = new XRecord();
        xr.record = r;
        xr.id = _record.id;
        xr.date2 = ConvertToDateString(_record.modifiedDate);

        return xr;
    }
私有静态XRecord GrabRecord(记录_记录)
{
XNamespace名称空间=”http://www..com/ws/schemas";
XDocument r=
新XDocument(
新XElement(命名空间+“getRecordsResponse”,
新的XAttribute(XNamespace.Xmlns+“ns1”,名称空间),
新XElement(“列表”,
新的元素(“ID”、_record.ID),
新XElement(“标题”,“记录标题”),
...............
新的元素(“注释”、_record.comments),
新元素(“类别”