在c# 3.0中,您可以使用以下语法创建匿名类
var o = new { Id = 1, Name = "Foo" };
有没有办法将这些匿名类添加到泛型列表中?
例子:
var o = new { Id = 1, Name = "Foo" };
var o1 = new { Id = 2, Name = "Bar" };
List<var> list = new List<var>();
list.Add(o);
list.Add(o1);
另一个例子:
List<var> list = new List<var>();
while (....)
{
....
list.Add(new {Id = x, Name = y});
....
}
根据这个答案,我提出了两种可以完成任务的方法:
/// <summary>
/// Create a list of the given anonymous class. <paramref name="definition"/> isn't called, it is only used
/// for the needed type inference. This overload is for when you don't have an instance of the anon class
/// and don't want to make one to make the list.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="definition"></param>
/// <returns></returns>
#pragma warning disable RECS0154 // Parameter is never used
public static List<T> CreateListOfAnonType<T>(Func<T> definition)
#pragma warning restore RECS0154 // Parameter is never used
{
return new List<T>();
}
/// <summary>
/// Create a list of the given anonymous class. <paramref name="definition"/> isn't added to the list, it is
/// only used for the needed type inference. This overload is for when you do have an instance of the anon
/// class and don't want the compiler to waste time making a temp class to define the type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="definition"></param>
/// <returns></returns>
#pragma warning disable RECS0154 // Parameter is never used
public static List<T> CreateListOfAnonType<T>(T definition)
#pragma warning restore RECS0154 // Parameter is never used
{
return new List<T>();
}
你可以使用这些方法
var emptyList = CreateListOfAnonType(()=>new { Id = default(int), Name = default(string) });
//or
var existingAnonInstance = new { Id = 59, Name = "Joe" };
var otherEmptyList = CreateListOfAnonType(existingAnonInstance);
这个答案也有类似的想法,但直到我创建了这些方法之后我才发现它。
有许多方法可以做到这一点,但这里的一些响应创建了一个包含垃圾元素的列表,这需要您清除该列表。
如果您正在寻找泛型类型的空列表,请对元组列表使用Select以生成空列表。没有元素将被实例化。
下面是创建空列表的一行代码:
var emptyList = new List<Tuple<int, string>>()
.Select(t => new { Id = t.Item1, Name = t.Item2 }).ToList();
然后你可以使用你的泛型类型添加到它:
emptyList.Add(new { Id = 1, Name = "foo" });
emptyList.Add(new { Id = 2, Name = "bar" });
作为一种替代方法,你可以像下面这样创建空列表(但是,我更喜欢第一个例子,因为你也可以将它用于一个填充的元组集合):
var emptyList = new List<object>()
.Select(t => new { Id = default(int), Name = default(string) }).ToList();