使用匿名类型实例化泛型类型

使用泛型构造函数需要命名匿名类型,这是不可能的。或者,可以使用通用方法来允许类型推断发生。

var anon = new { Foo = 1, Bar = 2 };
var anon2 = new { Foo = 5, Bar = 10 };
List<T> CreateList<T>(params T[] items) {
    return new List<T>(items);
}

var list1 = CreateList(anon, anon2);

List<T> 的情况下,隐式类型数组可以通过 ToList LINQ 方法转换为 List<T>

var list2 = new[] {anon, anon2}.ToList();