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
The problem I've run into is how to create a container for multiple ResourceFolder objects of different types.
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
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?
AlanPosted Nov 2, 2008, 3:09 PM
I don't think there is a workaround unless the data types have (or could be made to have) some relation to each other, such as implementing a common interface, which you could then use to constrain the generic type parameter, T.
You would then, of course, be able to call members of that interface within the generic class ResourceFolder.
JoelPosted Nov 3, 2008, 8:07 PM
I did think about using an interface, but to my knowledge, the only way to make that work would be to implement the interface once for each data type.
Rather than doing that, I think I'll just use a number of fields for each data type instead of one collection.
Thanks for the help!
(Also, for some annoying reason, posting a reply in Opera doesn't work.)