|
-
Feb 4th, 2008, 01:24 AM
#1
[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
-
Feb 4th, 2008, 01:27 AM
#2
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?
-
Feb 4th, 2008, 01:42 AM
#3
Re: [2.0] Generics Without Generics Syntax
If you answered "yes" to the previous question then try this:
CSharp Code:
// Create the general generic type. Type baseType = typeof(List<>); // Create the generic argument type. Type argumentType = Type.GetType("System.String"); // Create the specific generic type. Type derivedType = baseType.MakeGenericType(argumentType); // Invoke the default constructor of the specific generic type to create an instance. Object list = derivedType.InvokeMember(string.Empty, System.Reflection.BindingFlags.CreateInstance, null, null, null); // Confirm the type of the object created. MessageBox.Show(list.GetType().ToString());
-
Feb 4th, 2008, 03:16 AM
#4
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.:
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
-
Feb 4th, 2008, 03:22 AM
#5
Re: [2.0] Generics Without Generics Syntax
This is a wild guess but maybe:
-
Feb 6th, 2008, 12:08 AM
#6
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?
-
Feb 6th, 2008, 12:19 AM
#7
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.
-
Feb 6th, 2008, 03:24 AM
#8
Re: [2.0] Generics Without Generics Syntax
-
Feb 6th, 2008, 03:38 AM
#9
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:
// Create the general generic type. Type baseType = Type.GetType("System.Collections.Generic.List`1");
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|