[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
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?
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());
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. :D
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
Re: [2.0] Generics Without Generics Syntax
This is a wild guess but maybe:
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?
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.
Re: [2.0] Generics Without Generics Syntax
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");