Hello, I would like to create a helper function in the class, which can be used by multiple List types at the same, so how can I create initialize or create function to pass List<T>
where T
can be any type of class in C#?
You would have to create dynamic list type using Reflections.
string something = "Some String";
Type type = something.GetType(); //get type
Type listType = typeof(List<>).MakeGenericType(new [] { type } );
IList list = (IList)Activator.CreateInstance(listType);
The above method is useful when you want to create list of unknown type.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly