❔ How to reference a list of objects that derive from 'Class<T>', if T is different for all of them?

I have a manager class, Manager<T>. The child classes for this set T to their own type, due to a fun Unity singleton implementation. So you will see: - CameraManager: Manager<CameraManager> - GameManager: Manager<GameManager> Im trying some new things around my bootstrap process, and I wanted to be able to make a List<Manager> where I can iterate through them and call Manager<T>.Initialize(). However, my list declaration requires T to be quantified. Does anyone know how I might make the list accept the various generic implementations?
4 Replies
nukleer bomb
nukleer bomb2y ago
C# generics are very strict, and you can't ever refer to Manager class without specifying generic type As a workaround, create a non-generic base class for your Manager and define abstract Initialize method in it.
dracon_interactive
Ah right, makes sense. I was just hoping it was something I missed haha. Thanks for the advice!
Dusty
Dusty2y ago
You'd need a non generic base class or interface which you store and your list which defines the Initialize() method
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server