Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic list by using reflection

I have a scenario on which i got Class name as a string I just want to create a generic List of that class. I tried the following

            Type _type = Type.GetType(className);

            List<_type> list = new List<_type>();

But iam geting error. I can't gave that _type as list argument. If anybody knows please help me

like image 502
Nithesh Narayanan Avatar asked Dec 09 '22 22:12

Nithesh Narayanan


1 Answers

Closest you can get:

Type listType = typeof(List<>).MakeGenericType(_type);
IList list = (IList) Activator.CreateInstance(listType);
like image 133
Markus Jarderot Avatar answered Feb 06 '23 05:02

Markus Jarderot