2. , , 100%, List<int>, , . , , , . , , ... , ;)
Type userType = GetUserSuppliedType();
Type listOfUserType = typeof(List<>).MakeGenericType(new[] { userType });
object listObject = Activator.CreateInstance(listOfUserType);
MethodInfo addMethod = listOfUserType.GetMethod("Add");
object input = GetUserSuppliedInput();
if (input == null || input.GetType() != userType)
{
throw new InvalidOperationException("That isn't going to work!");
}
addMethod.Invoke(listObject, new[] { input });
: , , , , !
Type genericListType = typeof(List<>);
Type listOfInt32Type = genericListType.MakeGenericType(new[] { typeof(int) });
object listObject = Activator.CreateInstance(listOfInt32Type);
List<int> list = (List<int>)listObject;
list.Add(1);
Generics , Type . :
var list = new List<int>();
list.Add(1);
list , , List<int>, , list, Add(1).
:
Type t = GetTypeFromIndeterminateSourceSuchAsUserInput();
var list = new List<t>();
list.Add(?);
t Type, (, int), , , , , (. Andrey answer), - .
, - :
Type t = typeof(int);
var list = new List<t>();
list.Add(1);
... , t () , .
, , , ; , , . , , , .