You cannot assign a generic type at runtime because a genus type must be resolved at compile time.
You can create a generic type in a dynamic way using reflection, but if you don't produce hard code, all you get in return is an object.
, , , XY. , -, , , , .
, dynamic List<T> / , T:
using System;
using System.Collections.Generic;
namespace ConsoleApplication61
{
class Program
{
static void Main(string[] args)
{
dynamic o = CreateGeneric(typeof(List<>), typeof(int));
o.Add(1);
Console.WriteLine(o[0]);
Console.Read();
}
public static object CreateGeneric(Type generic, Type innerType, params object[] args)
{
System.Type specificType = generic.MakeGenericType(new System.Type[] { innerType });
return Activator.CreateInstance(specificType, args);
}
}
}
Add . DLR - , 1 int, .
, , , ( ), ; ( IntelliSense), .
CreateGeneric.
.NET 4 CLR. @MartinLiversage, , . int List<int>, dynamic.
.NET 4 . . dynamic . , " ", " , ".