Results 1 to 7 of 7

Thread: [2.0] Dynamic Generic List<T>

  1. #1

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    [2.0] Dynamic Generic List<T>

    Hi all,

    I have a situation such that it is only at run time that i would know what type of generic list i want to create. Therefore I need something like:

    Private List<IBase> Test(IBase myObj) // where IBase is a base class which all others inherit
    {
    Type myType = GetType(myObj)
    Return new List<myType>;
    }

    I know the code above is rubbish, its just to give you an idea of what i want to achieve. Any ideas???
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Dynamic Generic List<T>

    Bit hard to make a suggestion without knowing a bit more about the particulars of the situation, but what you could do is make a static function in each class that inherits from IBase (side note: the I- prefix usually denotes an interface, not an abstract class; consider changing this to avoid confusion) that returns a vector of that particular type.

    However, you should bear in mind that if you are only ever going to call members of IBase upon the elements contained in the list(s), then you stand to gain no benefit by using a more specific type than List<IBase>.

  3. #3

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Re: [2.0] Dynamic Generic List<T>

    Ok, here goes:

    IBase in an Interface Class defining methods and properties that all classes implements.

    BaseClass is a generic class that implements IBase

    Within BaseClass there's a method that looks like:

    public List<IBase> GetList(IBase obj)
    {
    //
    }

    ClassA, ClassB, ClassC etc inherits BaseClass

    Basically in the above, obj can be ClassA, ClassB or ClassC, etc...
    The idea is to have pass the appropriate class as parameter to the above method and it would in turn return a generic list of that class.
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2.0] Dynamic Generic List<T>

    Like this?

    Code:
        public static List<T> GetList<T>(int size)
        {
            return new List<T>(size);
        }

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Dynamic Generic List<T>

    What mendhak suggests will go part of the way but there's no way to limit what T can be, so that GetList method cannot be limited to returning Lists of types that implement IBase. Also, you couldn't do this:
    c# Code:
    1. List<IBase> list = GetList<ClassA>(100);
    because List<IBase> and List<ClassA> are different classes. Even though one type of item may inherit or implement the other, that doesn't mean that the collections themselves inplement or inherit each other.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Re: [2.0] Dynamic Generic List<T>

    Ok thats my point, so you can't return a list of say ClassA as being same as a List of IBASE. I'll have to find another way then...
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Dynamic Generic List<T>

    Generics make many situations easier that were tedious in .NET 1.x, but there are certain things that can't, and shouldn't, be possible. You'd basically have to break typing rules to achieve your aim. If you don't know the types of things until run time then the solution is reflection. It's tedious to do anything of any complexity but you can do anything you want without knowing the actual types until run time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width