以下是我的类,其中包含类别类型的别名List属性(MeetingAliases)。如何将多个项添加到别名并作为参数与其他TSGetRootObject属性数据一起传递给某个方法。例如。
TSGetRootObject ts = new TSGetRootObject();
ts.aliases ???? //How to add data list here
ts.name = id;
ts.service_type = srvtype;
ts = TransfomationSrv.PostData(ts);
public class TSGetRootObject
{
[DataMember]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<MeetingAliases> aliases { get; set; }
[DataMember]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public bool? allow_guests { get; set; }
}
public class MeetingAliases
{
[DataMember]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string alias { get; set; }
[DataMember]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string conference { get; set; }
[DataMember]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string description { get; set; }
[DataMember]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public int? id { get; set; }
}
答案 0 :(得分:2)
ts.aliases = new List<MeetingAliases>();
ts.aliases.Add(new MeetingAliases
{
alias = "Some text",
conference = "Other text"
});
我建议您查看Object Initializer
您还可以在aliases
的构造函数中初始化TSGetRootObject
列表:
public class TSGetRootObject
{
public TSGetRootObject
{
aliases = new List<MeetingAliases>();
}
//Or in c# 6.0 or higher:
public List<MeetingAliases> aliases { get; set; } = new List<MeetingAliases>();
}
答案 1 :(得分:0)
如何执行此操作的一个选项是:
在TSGetRootObject
初始化List
的位置添加构造函数。然后将新对象MeetingAliases
添加到TSGetRootObject
对象:
TSGetRootObject ts = new TSGetRootObject();
ts.aliases.Add(new MeetingAliases {
alias = "alias",
conference = "conference",
description = "description",
id = 1
});
ts.name = id;
ts.service_type = srvtype;
ts = TransfomationSrv.PostData(ts);
public class TSGetRootObject
{
[DataMember]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public List<MeetingAliases> aliases { get; set; }
[DataMember]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public bool? allow_guests { get; set; }
public TSGetRootObject(){
aliases = new List<MeetingAliases>();
}
}
public class MeetingAliases
{
[DataMember]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string alias { get; set; }
[DataMember]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string conference { get; set; }
[DataMember]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string description { get; set; }
[DataMember]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public int? id { get; set; }
}