I got a scenario to create the anonymous list from the anonymous types, and i achieved that using
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 });
}
these are the error messages
System.Collections.Generic.List.Add(AnonymousType#1)'
has some invalid arguments
Argument
'1': cannot convert from
'AnonymousType#2' to 'AnonymousType#1'
whats the reason behind that??
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…