首页 > 解决方案 > C# 问题实例化作为隐含数组的类

问题描述

我希望你能帮忙。我正在与英国的 Land Registry Business Gateway 合作。我有一个来自他们的 reference.cs 文件,其中包含从 Web 服务参考中添加的代码。我设法引用了模块的所有部分,除了一个。我在实例化对象时遇到问题,因为它是一个隐含的数组,并且在调用结束时不会采用 () 但需要 [2] (其中 2 是数组中的元素数。模块中的代码在这里:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3056.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.oscre.org/ns/eReg-Final/2011/RequestTitleKnownOfficialCopyV2_1")]
public partial class Q1ContactType : object, System.ComponentModel.INotifyPropertyChanged {

private Q3TextType nameField;

private Q1CommunicationType communicationField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public Q3TextType Name {
    get {
        return this.nameField;
    }
    set {
        this.nameField = value;
        this.RaisePropertyChanged("Name");
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order=1)]
public Q1CommunicationType Communication {
    get {
        return this.communicationField;
    }
    set {
        this.communicationField = value;
        this.RaisePropertyChanged("Communication");
    }
}

public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

protected void RaisePropertyChanged(string propertyName) {
    System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
    if ((propertyChanged != null)) {
        propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
    }
}
}

我的代码在这里:

request.Product.Contact = new Q1ContactType[2];
request.Product.Contact[0].Name = new LROfficialCopyTitleKnownV2_1.Q3TextType();
request.Product.Contact[0].Name.Value = "Allen Jones";

如果我在 Q1ContactType 之后使用 (),它会抛出一个错误,说它需要 []。如果我把 [] 没有数字,它会抛出一个错误,说必须初始化数组。如果我按照上面显示的方式运行它,我会收到错误 Object reference not set to an instance of an object 因为该对象尚未实例化。request.Product 指的是 xml 文档的第一部分。

标签: c#xml

解决方案


推荐阅读