tags:

views:

1081

answers:

1

At present I'm having to do something like this to build a Type definition at runtime to pass to my IOC to resolve. Simplified:

Type t = Type.GetType(
"System.Collections.Generic.List`1[[ConsoleApplication2.Program+Person");

I know the generic type argument at runtime only.

Is there something that will allow me to do something like this (fake code):

Type t = Type.GetTypeWithGenericTypeArguments(
    typeof(List)
    , passInType.GetType());

Or shall I just stick to my hack, passInType.GetType() convert to string, build generic type string.. feel dirty

+14  A: 

MakeGenericType - i.e.

Type passInType = ... /// perhaps myAssembly.GetType(
        "ConsoleApplication2.Program+Person")
Type t = typeof(List<>).MakeGenericType(passInType);

For a complete example:

using System;
using System.Collections.Generic;
using System.Reflection;
namespace ConsoleApplication2 {
 class Program {
   class Person {}
   static void Main(){
       Assembly myAssembly = typeof(Program).Assembly;
       Type passInType = myAssembly.GetType(
           "ConsoleApplication2.Program+Person");
       Type t = typeof(List<>).MakeGenericType(passInType);
   }
 }
}


As suggested in the comments - to explain, List<> is the open generic type - i.e. "List<T> without any specific T" (for multiple generic types, you just use commas - i.e. Dictionary<,>). When a T is specified (either through code, or via MakeGenericType) we get the closed generic type - for example, List<int>.

When using MakeGenericType, any generic type constraints are still enforced, but simply at runtime rather than at compile time.

Marc Gravell
Might be a good idea to add explanation of open/closed generic types for completeness.
Arnis L.
MakeGenericType was exactly what I was looking for. Thanks! :) Answer accepted