C# 根据WSDL初始化Soap请求的项时,对象引用未设置为对象的实例

C# 根据WSDL初始化Soap请求的项时,对象引用未设置为对象的实例,c#,.net,wsdl,C#,.net,Wsdl,我有一个WSDL,它包含OrderInformation\u type类,由三个私有属性组成,它们本身就是类:Header\u type,PromotionInformation\u type和一个ItemInformation\u type[]数组。我已将OrderInformation\u类型的对象初始化为“order”。此外,我还初始化了order.Header\u typeHeader和order.PromotionInformation\u type对象,这些对象已成功初始化,并且可以

我有一个WSDL,它包含
OrderInformation\u type
类,由三个私有属性组成,它们本身就是类:
Header\u type
PromotionInformation\u type
和一个
ItemInformation\u type[]
数组。我已将
OrderInformation\u类型的对象初始化为“order”。此外,我还初始化了
order.Header\u type
Header和
order.PromotionInformation\u type
对象,这些对象已成功初始化,并且可以轻松设置其属性。但是,当我尝试初始化
order.ItemInformation\u type[]
对象时,我在运行时遇到一个错误,指出对象引用未设置为
对象的实例。考虑到
OrderInformation\u type
具有
ItemInformation\u type
属性作为数组,因此我按以下方式初始化它:

WindowsFormsApplication1.ServiceReference1.OrderInformation_type orderinfo = new ServiceReference1.OrderInformation_type();
            orderinfo.Header = new ServiceReference1.Header_type();
            orderinfo.Header.AccountNumber = 496570;
            orderinfo.Header.DistributorIdentifier = ServiceReference1.Header_typeDistributorIdentifier.MBA;

            orderinfo.PromotionInformation = new ServiceReference1.PromotionInformation_type();
            **orderinfo.ItemInformation[0] = new ServiceReference1.ItemInformation_type();**
            orderinfo.ItemInformation[0].ItemID = "95847";
            orderinfo.ItemInformation[0].ItemIDType = ServiceReference1.ItemInformation_typeItemIDType.D;
            orderinfo.ItemInformation[0].Quantity = 1;

粗体是获取错误的行。

orderinfo.ItemInformation是一个数组。您正在尝试将尚未创建的内容分配给[0]。添加

orderinfo.ItemInformation=新建 服务参考1.促销信息类型[]


应该这样做…

除非您有构造函数,
新服务引用1.OrderInformation_type()
不会自动创建ItemInformation数组(或列表)。正在初始化其余两个的可能重复项,标头和促销信息,但ItemInformation没有。这些类是从wsdl自动生成的。jt怎么会错过构造函数呢?我认为属性是数组存在问题。由于元素可以存在多次,如wsdl中所示,您找到了解决此问题的方法吗?ServiceReference1.ItemInformation=new ServiceReference1.ItemInformation[];是的,它确实为对象Error的实例提供了相同的not set,该行应该位于引发异常的行之前。还要确保新异常位于同一行。如果您已初始化数组,则该异常在该行没有意义。不,该行中应包含一个数字:
newservicerence1.ItemInformation[10]