2
Answers

Creating collections of generics of multiple types?

Joel

Joel

16y
2.2k
1

Hello,

I am attempting to build something of a resource management system in which a generic type holds the data and file locations of a number of resources of an arbitrary type.
I'll call it ResourceFolder<T> where T is the type of data it holds.

The problem I've run into is how to create a container for multiple ResourceFolder objects of different types. <br>
For example, this container might contain a folder of Texture objects and another of Model objects.

Because C# does not yet support variance for generics, I can't use something like
Dictionary<Type, ResourceFolder<object>>
to store these objects without some sort of workaround.

The only workarounds I have seen require converting the entire generic manually.  With a structure that can potentially hold very large amounts of data, I don't really want to do this.

The only other solution I have come up with is to make ResourceFolder a non-generic type that knows the type of the resources it contains, but that makes a number of other things more cumbersome than they need to be.

Is there any way to work around this?

 

Answers (2)