【发布时间】:2016-04-22 02:25:30
【问题描述】:
我有一个类如下,它是一个API,所以它必须是这种格式
public class Command
{
public string response_type { get; set; }
public string text { get; set; }
public Attachment[] attachments { get; set; } = new Attachment[] { new Attachment { } };
}
public class Attachment
{
public string title { get; set; }
public string title_link { get; set; }
public string image_url { get; set; }
}
所以它是一个 response_type、文本和附件数组。你可以看到我创建了附件数组并创建了一个空对象。
创建对象时,数组中永远只有一个元素。
在声明对象时如何设置或添加到数组中,因为对象已经在构造函数中创建了
Command result = new Command()
{
text = "Rebooting!",
attachments[0] = ????
};
我错过了一些简单的东西,尝试了很多组合
【问题讨论】:
-
您想在运行时将元素添加到数组中吗?
-
您可以创建带参数的构造函数。其中一个参数可以是数组。您的构造函数在参数列表中没有任何参数。