私は持っている:
class Car {..}
class Other{
List<T> GetAll(){..}
}
私はやってみたいです:
Type t = typeof(Car);
List<t> Cars = GetAll<t>();
これどうやってするの?
リフレクションを使用して、実行時に発見した型のデータベースからジェネリック コレクションを返したいと考えています。
Type generic = typeof(List<>);
Type specific = generic.MakeGenericType(typeof(int));
ConstructorInfo ci = specific.GetConstructor(Type.EmptyTypes);
object o = ci.Invoke(new object[] { });
これにはリフレクションを使用できます。
Type t = typeof(Car);
System.Type genericType= generic.MakeGenericType(new System.Type[] { t});
Activator.CreateInstance(genericType, args);