You can box and unbox types like this:
List<object> items = new List<object>();
items.Add("hello");
items.Add(123);
Here, it doesn't matter what we put in, as long as it derives from object. So this works.
But is it possible to do it with generic classes? Like this:
public class Foo<T>
{
public T MyItem;
}
static void Main()
{
List<Foo<object>> items = new List<Foo<object>>();
items.Add(new Foo<string>() { MyItem = "Hello" });
items.Add(new Foo<int>() { MyItem = 123 });
}
Here, it will give me an error despite string and int is type of object.
There is one easy solution that i have thought about, and that is by turning new Foo< string > into new Foo< object >, and then just put in string value in the object type like this:
items.Add(new Foo<object>() { MyItem = "Hello" });
But i'm in a situation where i cant do that.
So is there any solution to make this possible?