【发布时间】:2020-06-18 14:30:28
【问题描述】:
嘿,美妙的 stackoverflowers, 我在使用作为类的一部分的 IList 对象时遇到了一些问题。 稍后它将被序列化为 JSON,其中要求它将在 customfield_10304 对象内生成一个包含键值对的数组。
习惯于使用字典来实现类似的目的,我在尝试使用 IList 做类似的事情但失败了。
public class Customfield
{
public string self { get; set; }
public string value { get; set; }
public string id { get; set; }
}
public class RequestFieldValues
{
public IList<Customfield> customfield_10304 { get; set; }
}
/* NOTE: This is how I THINK it should work in my mind, but it is throwing errors */
var customfield_10304 = new IList<string> { {value = "test", id = 0} }
解决这个问题的好方法是什么?请指导我找到最合适的解决方案。
提前谢谢你
【问题讨论】:
-
你不能创建接口的实例。尝试创建一个实现 IList 的类的实例,例如 List。查看 documentation 了解实现 IList 的类
-
要序列化成 JSON,请参考这些链接 newtonJson 和 Serialization in .NET。此外,JSON 序列化会生成与 Dictionary 不同的键值对。
标签: c# list instantiation