14

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

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

34

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
  • 1,026,079
  • 266
  • 2,566
  • 2,900