【发布时间】:2010-01-28 12:22:15
【问题描述】:
我有一个从匿名类型创建匿名列表的场景,我使用
实现了这一点 public static List<T> MakeList<T>(T itemOftype)
{
List<T> newList = new List<T>();
return newList;
}
static void Main(string[] args)
{
//anonymos type
var xx = new
{
offsetname = x.offName,
RO = y.RO1
};
//anonymos list
var customlist = MakeList(xx);
//It throws an error because i have given the wrong order
customlist.Add(new { RO = y.RO2, offsetname = x.offName });
customlist.Add(new { RO = y.RO3, offsetname = x.offName });
//but this works
customlist.Add(new { offsetname = x.offName, RO = y.RO2 });
customlist.Add(new { offsetname = x.offName, RO = y.RO3 });
}
这些是错误信息
System.Collections.Generic.List.Add(AnonymousType#1)' 有一些无效的参数
论据 '1': 不能从 'AnonymousType#2' 到 'AnonymousType#1'
这背后的原因是什么?
【问题讨论】:
标签: c# anonymous-types