我想创建一个通用列表<>,其类型在运行时声明。C#动态通用列表
我可以做以下事情,但由于它是动态的,我怀疑有速度的损失。我正在写一个外来数据库的包装,所以速度至关重要。
List<dynamic> gdb = new List<dynamic>()
我读动态泛型类型this post,但不能让它工作。具体而言,该对象不会显示为List,因此不具有添加方法。
Type ac;
switch (trail[dataPos].Type)
{
case GlobalsSubscriptTypes.Int32:
ac = typeof(System.Int32);
break;
case GlobalsSubscriptTypes.Int64:
ac = typeof(System.Int64);
break;
default:
ac = typeof(System.String);
break;
}
var genericListType = typeof(List<>);
var specificListType = genericListType.MakeGenericType(ac);
var gdb = Activator.CreateInstance(specificListType);
如何获得GDB显示为下列之一:
List<System.Int32>
List<System.Int64>
List<System.String>
它在这种情况下使用'List
不是一个坏主意,但我希望列表被键入,因为它将成为查询的一部分。 – IamIC
列表怎么样? –