Original Post
Hey Folks, lately I've been experimenting with C# and generics and I ran into a problem I am almost certain C# has an answer for but I just cannot get to it. Maybe one of you could gimme a hint of how I should tackle that problem. Here we go: I have many many objects that all have something in common. They have a name and a value. However this value is of different type. Possible types are Int32, Int16, Double, ..., and many many more!. I would like to save all those objects in one List. A possible way to do that would be: The problem with this approach is, that I cannot benefit from the generics, since I always have to cast the A object into a B object, where I would have to know the type of B >! Is there a way to use the generics in that problem? Mabye with MetaData, Interfaces or something else? I hope I described the problem sufficiantly. Any hint/link/advice is appreciated!!! Cherio Woltan
class A
{
public static List Objects = new List();
public String Name {get;set;}
protected A()
{
Objects.Add(this);
}
}
class B<T> : A
{
public T Value {get;set;}
}
void Func()
{
B<?> b = (B<?>)A.Objects[0];
b.Value = some_data_of_some_type;
}