[英]How to initialize an array in a property's sub-class? (Object reference not set… error)
首先,请坚持我如何使用当前设置完成此操作,而不是尝试重新设计它的结构。 我必须构建一个应用程序/Web 服务来接收 JSON 查询。 JSON 是由我们无法自定义的小部件生成的。 我使用 Visual Studio 从 JSON 示例创建 class 结构。
我想测试我的服务,我在 SOAPUI 中遇到错误,所以我需要一些可以调试的东西,所以我正在硬编码 JSON 输入 object 以作为我的服务参考。
这是类,因为它们驻留在 web 服务上 -
[DataContract]
public class QuoterIn
{
[DataMember]
public Quoterinput quoterinput { get; set; }
}
public class Quoterinput
{
public string agent_id { get; set; }
public string key { get; set; }
public string auto_assign { get; set; }
public Consumer consumer { get; set; }
public string viaEmail { get; set; }
}
public class Consumer
{
public string brand_id { get; set; }
public string full_name { get; set; }
public Addresses_Attributes[] addresses_attributes { get; set; }
public string birth_or_trust_date { get; set; }
public string gender { get; set; }
public Emails_Attributes[] emails_attributes { get; set; }
public Phones_Attributes[] phones_attributes { get; set; }
public Cases_Attributes[] cases_attributes { get; set; }
}
public class Addresses_Attributes
{
public string zip { get; set; }
}
public class Emails_Attributes
{
public string value { get; set; }
}
public class Phones_Attributes
{
public string value { get; set; }
}
public class Cases_Attributes
{
public Quoting_Details_Attributes[] quoting_details_attributes { get; set; }
}
public class Quoting_Details_Attributes
{
public string carrier_id { get; set; }
public string duration_id { get; set; }
public string policy_type_id { get; set; }
public string plan_name { get; set; }
public string product_type_name { get; set; }
public string face_amount { get; set; }
public string carrier_health_class { get; set; }
public string planned_modal_premium { get; set; }
public string premium_mode_id { get; set; }
public string health_class_id { get; set; }
}
查看Consumer的子类,有几个子类是arrays——address_attributes、phone_attributes和email_attributes。
我能够初始化主 JSON 输入/查询容器,然后我可以初始化主 class 和“消费者”的子类。 我可以为主要字段输入数据,然后在 Consumer 的子类中输入字段,但我无法初始化 arrays。
这是到目前为止用于填充该输入容器的代码 -
protected void btnRun_Click(object sender, EventArgs e)
{
QuoterIn req = new QuoterIn();
req.quoterinput = new Quoterinput();
req.quoterinput.consumer = new Consumer();
req.quoterinput.agent_id = "1938";
req.quoterinput.key = "afasdfasdfasdfasdfasd";
req.quoterinput.auto_assign="true";
req.quoterinput.consumer.brand_id = "21264";
req.quoterinput.consumer.full_name = "Fred Smith";
req.quoterinput.consumer.addresses_attributes[0].zip = "53704";
.................(more code like this, but this last line is where the program fails)
因为我们不打算接收多个 email 地址或电话号码,所以我可以不用它是一个数组,但我担心传入的 JSON 如果没有设置所有分组和包围将失败像一个数组一样。 我试图像这样初始化实例零 -
req.quoterinput.consumer.addresses_attributes[0] = new Addresses_Attributes();
.... 并且 Visual Studio 喜欢代码语法,但它在运行时不会初始化实例,然后我得到“对象引用未设置为 object 的实例。” 运行时出错。 它不允许我使用更通用的方法初始化数组,因为它必须 map 到我们已经声明的这个特定属性。
我毫不怀疑这可能很简单,但是我在操作 arrays 和这样的列表方面并不是很有经验,而且我还没有找到 arrays 的子类在属性子类中的示例。
只需同时实例化/分配地址。 像这样的东西应该工作:
req.quoterinput.consumer.addresses_attributes = new []
{
new Addresses_Attributes
{
zip = "53704"
}
};
另一种方法(因为玩具试图设置的属性是一个数组),是创建一个动态大小的列表,添加到它,然后在声明期间将其转换回来。
var addresses = new List<Addresses_Attributes>
{
new Addresses_Attributes {zip = "53704"}
};
req.quoterinput.consumer.addresses_attributes = addresses.ToArray();
或者,您可以为其分配一个大小为 1 的数组,然后从那里进行设置:
req.quoterinput.consumer.addresses_attributes = new Addresses_Attributes[1];
req.quoterinput.consumer.addresses_attributes[0] = new Addresses_Attributes { zip = "666" };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.