Results 1 to 9 of 9

Thread: [RESOLVED] [2.0] Generics Without Generics Syntax

  1. #1

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Resolved [RESOLVED] [2.0] Generics Without Generics Syntax

    All,


    I know that a class's constructor is callable via a hidden New method.

    Is there a way of constructing a generic object using this type of hidden method?
    I don't have access to a reflector right now, so I can't examine all the built-in methods.

    (This is a general .NET question, not really a C# one. I'm not using C#, which is why I can't use the ClassName<type> syntax.)


    Something along the lines of:
    Code:
    obj = List.NewGeneric('System.String')
    —would be great.


    Cheers,

    — P

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

    Re: [2.0] Generics Without Generics Syntax

    I'm not 100% sure what you mean. Are you saying that you want to, for example, create a new List<String> by specifying the generic type as a string at run time rather than as a type at design 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

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

    Re: [2.0] Generics Without Generics Syntax

    If you answered "yes" to the previous question then try this:
    CSharp Code:
    1. // Create the general generic type.
    2. Type baseType = typeof(List<>);
    3.  
    4. // Create the generic argument type.
    5. Type argumentType = Type.GetType("System.String");
    6.  
    7. // Create the specific generic type.
    8. Type derivedType = baseType.MakeGenericType(argumentType);
    9.  
    10. // Invoke the default constructor of the specific generic type to create an instance.
    11. Object list = derivedType.InvokeMember(string.Empty,
    12.                                        System.Reflection.BindingFlags.CreateInstance,
    13.                                        null,
    14.                                        null,
    15.                                        null);
    16.  
    17. // Confirm the type of the object created.
    18. MessageBox.Show(list.GetType().ToString());
    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

  4. #4

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Generics Without Generics Syntax

    That's almost, but not quite, what I meant.
    Sorry, I typed the question in a rush to leave work.

    I'm actually using APL. This has no native support for generics, so it has no generic syntax — but it is loosely typed, so it can support any .NET object once it is instantiated.

    My problem is instantiating the object.
    I can instantiate non-generic types by using the hidden New method, e.g.:
    APL Code:
    1. X ← ArrayList.New

    I have no idea how to do that for a generic type, which is why I was looking for some other static method that might be present in the MSIL but not exposed in the Visual Studio IDE.


    It isn't essential that I use generics, but it would be nice, since I hate using late-bound collections when generics are available.


    (Your sample is interesting. I will try it, but can't quite at the moment guess how to convert the 'typeof' line.)


    Thanks for the reply.

    — P

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

    Re: [2.0] Generics Without Generics Syntax

    This is a wild guess but maybe:
    Code:
    X ← List`1[String]
    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
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Generics Without Generics Syntax

    J,

    On line two of your snippet you use typeof(List<>).

    Is there a string equivalent for the List<> part, similar to how you've done it on line 5?

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

    Re: [2.0] Generics Without Generics Syntax

    It would be "List`1", i.e. the List class with one generic type parameter. Note that that's not a single quote. It's the one on the same key as the tilde.
    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

  8. #8

    Thread Starter
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Generics Without Generics Syntax

    Perfect. Thanks.

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

    Re: [RESOLVED] [2.0] Generics Without Generics Syntax

    Just did a quick test and, in C# at least, the class name has to be fully qualified in the string:
    CSharp Code:
    1. // Create the general generic type.
    2. Type baseType = Type.GetType("System.Collections.Generic.List`1");
    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