How to create a dynamic type List <T>

I do not want my list to have a fixed type. Rather, I want the list creation to depend on the type of variable. This code does not work:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.Generic; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { string something = "Apple"; Type type = something.GetType(); List<type> list = null; Console.ReadKey(); } } } 

Can someone tell me what changes I need to make to make it work correctly? I want the creation of list depend on the type of the variable something

+6
source share
4 answers
 string something = "Apple"; Type type = something.GetType(); Type listType = typeof(List<>).MakeGenericType(new [] { type } ); IList list = (IList)Activator.CreateInstance(listType); 

This is how you create a list of statically unknown types. But note that you cannot set a static list type at runtime. You should not use a generic type or even an object.

Without knowing more about what you want to achieve, this is the best you can do.

+27
source

I want security type, but I need dynamic type security.

If you want you to execute safe runtime, you can create a List<T> using reflection (see usr answer) or dynamic , and then treat it as a non-generic IList .

Using dynamic , it will look something like this:

 static List<T> CreateListByExample<T>(T obj) { return new List<T>(); } … object something = "Apple"; IList list = CreateListByExample((dynamic)something); list.Add(something); // OK list.Add(42); // throws ArgumentException 
+5
source

Dynamics and reflection all work fine - but with a lack of performance - and lose strong typing, code design / clarity , etc.

those. you should always try to solve problems without it - if you can, your code allows this ...
So, and (note) depends (very much) on your specific code , needs,
you can also use the "trick" to "infer" the type and make it general ...

 class Program { static void Main(string[] args) { string something = "Apple"; int test = 5; var list = something.GetList(); var listint = test.GetList(); Console.WriteLine(list.GetType()); } } static class Extension { public static List<T> GetList<T>(this T value) { return new[] { value }.ToList(); } } 

... i.e. if you have a value for the variable and before the "input" of the general context,
you can use extensions (which are very useful for this) and let it print the type and type of the list for you
NOTE: this "work around", unfortunately, does not always end, and when your code is "too dynamic" (I know that it is not too "exact", but not appropriate for this), and if it depends on the types caused by reflection etc..
that is, there is no clean solution, this is just an example, you need to put a little sweat in it :) to make it work for you - for example, you may need a wrapper type here and there, and, obviously, creating a list this way can be not the way you want, etc.

+1
source

The compiler must know the generic type T at compile time. So no, you cannot do this.

0
source

Source: https://habr.com/ru/post/911599/


All Articles