none
GenericCollection Strongly Typed on Retreval RRS feed

  • General discussion

  • When using Generics i keep hitting a situation where i'd like to be able to collectively store generic classes with out reference to the typed constraints applied to generics at the point of storing

     

    The solution that seems feasible to me would be a Collection<TGeneric<>>:KeyedCollection<typeof(TType), TGeneric<TType>> where TGeneric would be the Generic class and TType would be the type applied to it

    so as an example you have some generic class CLASS<T>{}

    you create

    GenericCollection<CLASS<>> col

    then

    Col.Add(new CLASS<int>)

    Col.Add(new CLASS<double>)

    Col.Add(new CLASS<string>)

    then if you do

    Col[int]

    you would get back the CLASS<int> object in a type safe format

     

    the question is is this possible under the CLR with out using refection (as far as I'm aware not, but i may be wrong)

    if so how would you do it and if not does anyone else think it would be a valid and useful addition


    Definition of a Beginner
    Someone that is unaware of the rules that need to be obeyed

    Definition of an Expert
    Someone that know when and which rules to ignore

    • Edited by MIkeTims Tuesday, April 12, 2011 3:05 PM
    Tuesday, April 12, 2011 2:30 PM

All replies

  • Also would be Even more useful if you could stack it ie

    GenericCollection<List<CLASS<>>> col

     

    then you

    col{int] and get a List<Class<int>> returned


    Definition of a Beginner
    Someone that is unaware of the rules that need to be obeyed

    Definition of an Expert
    Someone that know when and which rules to ignore
    Tuesday, April 12, 2011 2:35 PM
  • You could, potentially, make a collection that wrapped up a Dictionary<Type, IList> internally, and generated a List<T> which was stored in that "slot" based on the requested type.  The code could work very similar to how you have it above, though you'd need to reference it as either:

     

        List<Class<int>> list = col.Get<int>();

    Or:

        List<Class<int>> list = col[typeof(int)];

     

     

    However, I question the usefulness of this in general.  I think you'll find that consuming this class is going to be difficult.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, April 12, 2011 3:54 PM
    Moderator
  • You could, potentially, make a collection that wrapped up a Dictionary<Type, IList> internally, and generated a List<T> which was stored in that "slot" based on the requested type.



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    that wouldn't be Type safe though as any IList could be added meaning that it would be unsafe to assume what was coming out was a List<Class<int>>

    in which case you might as well use a HashTable or ArrayList

     

    I've actually build classes for my own needs that do this but i'm having to make heavy use of reflection and have all the boxing issues because there is no way to specify that the content will be a member of a generic with out reference to the Type.

     

    and a quick search of the web says that this is a very common issue people are coming up against when using generics, the need to save generics in a common store either by creating a collection of CLASS<object> (or some other common base class) and trying to save an object of CLASS<int> to it and failing.

     

    The situation i'm in is trying to build a highly customisable system that allows users to build dynamic objects and define their behaviour in a controlled environment. due to the nature of this i have 2 options either save everything as objects and manually figure out all types  however this is inherently unstable as there can be no complier checking of type safety or as i've done use generics to store flexible objects that are type safe which are then stored in unboxed collections which is much more stable as it only require the programmer to check type safety when retrieving from the collections.

    Of course the Ideal solution would be able to make the collections type safe but as you have just agreed the current structures and rules for the CLR prevent this.

    Now i fully understand that microsoft can't support every hair brained idea that some programmer with a bee in their bonnets suggests to solve their specific issue. but i've hit this one often enough and seen enough others that have to, to think that this would be an improvement so i'm here making the suggestion so that if microsoft agree it can be added to future .net releases. if not then i just carry on with the work around i have


    Definition of a Beginner
    Someone that is unaware of the rules that need to be obeyed

    Definition of an Expert
    Someone that know when and which rules to ignore
    Wednesday, April 13, 2011 11:55 AM
  • that wouldn't be Type safe though as any IList could be added meaning that it would be unsafe to assume what was coming out was a List<Class<int>>

    in which case you might as well use a HashTable or ArrayList

     

    Mike,

     

    Internally, you're correct - there is no way for this to be typesafe.


    However, I was suggesting wrapping this in a class that would guarantee the constraints for you - so the consumer wouldn't add a list - but it'd be generated by the class.  By using Dictionary<Type, IList>, you'd be guaranteed (provided the wrapper manages the private dictionary), on retrieval, to retrieve the correct, type safe generic collection.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, April 13, 2011 4:05 PM
    Moderator