在运行时指定generics集合类型参数
我有:
class Car {..} class Other{ List GetAll(){..} }
我想要做:
Type t = typeof(Car); List Cars = GetAll();
我怎样才能做到这一点?
我想从我在运行时使用reflection发现的类型的数据库中返回一个generics集合。
Type generic = typeof(List<>); Type specific = generic.MakeGenericType(typeof(int)); ConstructorInfo ci = specific.GetConstructor(Type.EmptyTypes); object o = ci.Invoke(new object[] { });
你可以使用reflection:
Type t = typeof(Car); System.Type genericType= generic.MakeGenericType(new System.Type[] { t}); Activator.CreateInstance(genericType, args);
- 从LINQ查询中获取第一个结果 – 当First ()成功时,为什么ElementAt (0)会失败?
- 使用DeviceCreationFlags.Debug启动SharpDX / DirectX应用程序
- 是否可以在不使用win32 API库的情况下获取在C#中连接的IP地址列表。
- 从Temporary ASP.NET Files清除冲突的类文件
- 在C#中将XML字符串解析为类?
- Linq-to-SQL:组合(OR’ing)多个“包含”filter?
- entity framework:路径中的非法字符。 (连接字符串)(MVC3)
- 使用.NET中的类型化数据集将SQL参数传递给IN()子句
- C#generics字符串解析为任何对象