I don\'t want my List to be of fixed type. Rather I want the creation of List to be dependent on the type of variable. This code doesn\'t work:
using System;
I want type safety but I need dynamic type safety.
If you mean you want runtime type-safety, you can create List
using reflection (see usr's answer) or dynamic
and then treat it as the non-generic IList
.
Using dynamic
, it would look something like this:
static List CreateListByExample(T obj)
{
return new List();
}
…
object something = "Apple";
IList list = CreateListByExample((dynamic)something);
list.Add(something); // OK
list.Add(42); // throws ArgumentException