【发布时间】:2015-11-16 15:58:11
【问题描述】:
我需要创建一个返回动态[]的函数
这对我来说很好用
public static dynamic[] GetDonutSeries(this Polls p)
{
return new dynamic[]
{
new {category = "Football",value = 35},
new {category = "Basketball",value = 25},
new {category = "Volleyball",value = 20},
new {category = "Rugby",value = 10},
new {category = "Tennis",value = 10}
};
}
但我需要添加执行不同操作的项目。
这样
public static dynamic[] GetDonutSeries(this Polls p)
{
dynamic[] result = new dynamic[]();
foreach (PollOptions po in p.PollOptions)
{
result.Add(new {category = po.OptionName,value = po.PollVotes.Count});
}
return result;
}
但我不能对动态[] 使用 .Add 方法。我该怎么做?
【问题讨论】:
-
dynamic[] result = new dynamic[]();是否编译?
标签: c# arrays oop dynamic anonymous-types