This is what I want to do in C# (within class Helper - without generic arguments),
List<AbstractClass<dynamic>> data; public void Add<T>(AbstractClass<T> thing) { this.data.Add((AbstractClass<dynamic>) thing); } This helper class would take and work with AbstractClass<> objects and give back AbstractClass<> of specific generic type. AbstractClass<T> contains many functions which return T / take in T like public T Invoke(). For Helper class T cannot be known beforehand. The Add<T>(.. thing) function is not in a class of type T. To be used like this in Helper class's functions,
foreach(var c in data.Where(x => ...)) { // public T Invoke() { ... } function within AbstractClass<T> var b = c.Invoke(); // logic } This also fails,
List<AbstractClass<object>> data; public void Add<T>(AbstractClass<T> thing) { this.data.Add((AbstractClass<object>) thing); } Now I think I can have,
List<dynamic> data; // or List<object> data; public void Add<T>(AbstractClass<T> thing) { this.data.Add(thing); } but I want the constraint that List named data has only elements of type like
ConcreteClass : AbstractClass<OtherClass> So we would know that there is an public T Invoke() function but we do not know what it returns. This is helpful to avoid mistakes of say misspelling Invocke and only knowing at run-time.
I want to avoid casting to dynamic every time to invoke functions that give back generic type T
List<AbstractClass<T>> data;?List<..> datais not of type T. TheAdd<T>(... thing)function must handle adding many different concrete objects which extend the AbstractClass while not knowing what T is. I hope that makes some sense. That isclass Helper { ... List<..> data; public void Add<T>(... data) { ... } .. }