Dynamic Types in Generics?
-
Hello people, Is it posible to declare the type of a generic at runtime? I mean, you normaly would have to do something like this: GenericMethod<Type>(); And I want to do something like this: GenericMethod<(expression that evaluates to a type)>(); I have tried a few things but nothing seems to work. MSDN's C# Programming Guide states that: "client code must declare and instantiate a constructed type by specifying a type argument inside the angle brackets. The type argument for this particular class can be any type recognized by the compiler." Thanks!
-
Hello people, Is it posible to declare the type of a generic at runtime? I mean, you normaly would have to do something like this: GenericMethod<Type>(); And I want to do something like this: GenericMethod<(expression that evaluates to a type)>(); I have tried a few things but nothing seems to work. MSDN's C# Programming Guide states that: "client code must declare and instantiate a constructed type by specifying a type argument inside the angle brackets. The type argument for this particular class can be any type recognized by the compiler." Thanks!
You could always use a Func here. For instance:
GenericMethod<Func<T, TResult>>();
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
You could always use a Func here. For instance:
GenericMethod<Func<T, TResult>>();
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Good link Pete, very concise. :-D
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Hello people, Is it posible to declare the type of a generic at runtime? I mean, you normaly would have to do something like this: GenericMethod<Type>(); And I want to do something like this: GenericMethod<(expression that evaluates to a type)>(); I have tried a few things but nothing seems to work. MSDN's C# Programming Guide states that: "client code must declare and instantiate a constructed type by specifying a type argument inside the angle brackets. The type argument for this particular class can be any type recognized by the compiler." Thanks!
What kind of expression that evaluates to a type would you have there? Something with reflection? You could probably use reflection to invoke the method with an expression of the type
Type
, but I'm not actually sure. Reflection can do a lot though, so why not.. edit: If all else fails, there is still the possibility to compile and run a piece of code that has been generated at runtime. But I wouldn't use that except in emergencies. It's not exactly fast, or a clean design. -
What kind of expression that evaluates to a type would you have there? Something with reflection? You could probably use reflection to invoke the method with an expression of the type
Type
, but I'm not actually sure. Reflection can do a lot though, so why not.. edit: If all else fails, there is still the possibility to compile and run a piece of code that has been generated at runtime. But I wouldn't use that except in emergencies. It's not exactly fast, or a clean design.A method which may prove useful is Type.MakeGenericType[^]. I don't fully understand it, but it seems to be a step in the right direction - getting the type of a generic type (without the type specifiers), then calling MakeGenericType on the result to get a dynamically set Type
Between the idea And the reality Between the motion And the act Falls the Shadow
-
Hello people, Is it posible to declare the type of a generic at runtime? I mean, you normaly would have to do something like this: GenericMethod<Type>(); And I want to do something like this: GenericMethod<(expression that evaluates to a type)>(); I have tried a few things but nothing seems to work. MSDN's C# Programming Guide states that: "client code must declare and instantiate a constructed type by specifying a type argument inside the angle brackets. The type argument for this particular class can be any type recognized by the compiler." Thanks!
This code creates a generic type and instatiate an object of that type in runtime using MakeGenericType as Computafreak mentioned. I was going to post it last night but I forgot and fell asleep. But now I know you want it in .net 2.0 I have to remove
var
andnew[]
static void Main(string[] args)
{
Type t = typeof(List<> );
Type gt = t.MakeGenericType(GetTypes());
object gtObj = Activator.CreateInstance(gt);
}private static Type[] GetTypes()
{
return new Type[] { typeof(int) };
}But gtObj is returned as an object (but it is a List). The problem is that you will probably have to use reflection.
Eslam Afifi
-
This code creates a generic type and instatiate an object of that type in runtime using MakeGenericType as Computafreak mentioned. I was going to post it last night but I forgot and fell asleep. But now I know you want it in .net 2.0 I have to remove
var
andnew[]
static void Main(string[] args)
{
Type t = typeof(List<> );
Type gt = t.MakeGenericType(GetTypes());
object gtObj = Activator.CreateInstance(gt);
}private static Type[] GetTypes()
{
return new Type[] { typeof(int) };
}But gtObj is returned as an object (but it is a List). The problem is that you will probably have to use reflection.
Eslam Afifi
-
A method which may prove useful is Type.MakeGenericType[^]. I don't fully understand it, but it seems to be a step in the right direction - getting the type of a generic type (without the type specifiers), then calling MakeGenericType on the result to get a dynamically set Type
Between the idea And the reality Between the motion And the act Falls the Shadow
-
You're welcome. I read your other post[^] and you don't have to use the FullName of the Type, you can just pass the Type directly which is faster IMHO.
Eslam Afifi
-
You're welcome. I read your other post[^] and you don't have to use the FullName of the Type, you can just pass the Type directly which is faster IMHO.
Eslam Afifi