title: Using Reflection.Emit to instantiate Generic Type with Generic Parameters
body: <p>my goal is to use reflection emit to construct generic type with the generic parameters of the created generic method
  so the end result of the created generic method is similar to</p>
  
  <pre><code>void DoSomeThing&lt;T&gt;(T arg){ 
      var list=new List&lt;T&gt;();
  }
  </code></pre>
  
  <p>so what I need is the code used to emit this fragment of code</p>
  
  <pre><code>    new List&lt;T&gt;
  </code></pre>
  
  <p>and this is my try</p>
  
  <pre><code>        var _assemblyName = "asm.dll";
          var _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(_assemblyName), System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave);
          // ApplyReflectionPermission(asm); 
  
          var _moduleBuilder = _assemblyBuilder.DefineDynamicModule("module", _assemblyName, true);
  
  
          var type = _moduleBuilder.DefineType("type");
  
          var method = type.DefineMethod("DoSomeThing", MethodAttributes.Public | MethodAttributes.Static);
          var genericPrms = method.DefineGenericParameters("T");
          method.SetParameters(genericPrms);
          method.SetReturnType(typeof(void));
  
  
          var il = method.GetILGenerator();
          var listType = typeof(List&lt;&gt;);
          var list_of_T = listType.MakeGenericType(genericPrms);
  
  
          il.DeclareLocal(list_of_T);
          var c = list_of_T.GetConstructor(new Type[0]);
  
  
          il.Emit(OpCodes.Newobj, c);
          il.Emit(OpCodes.Stloc, 0);
          il.Emit(OpCodes.Ret);
          type.CreateType();
          _assemblyBuilder.Save(_assemblyName);
  </code></pre>
  
  <p>the exception is in this line of code</p>
  
  <pre><code>var c = list_of_T.GetConstructor(new Type[0]);
  </code></pre>
  
  <p>and it is caused by this line of code</p>
  
  <pre><code>var list_of_T = listType.MakeGenericType(genericPrms);
  </code></pre>
  
  <p>the exception is </p>
  
  <pre><code>System.NotSupportedException: Specified method is not supported.
     at System.Reflection.Emit.TypeBuilderInstantiation.GetConstructorImpl(BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers) 
     at System.Type.GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)    
     at System.Type.GetConstructor(Type[] types)
  </code></pre>
  
  <p>and by digging in (MakeGenericType) method, it returns a new instanse of TypeBuilderInstantiation if any of the parameter types isn't a (RuntimeType)</p>
  
  <p>the type TypeBuilderInstantiation is nothing but an empty implementation of the abstract type 'TypeInfo' [whis is an abstract impl. of the type 'Type'], which all of its method throwing not supported exception</p>
  
  <p>my goal is not to create a method to return new List, it's more complicated than that, but my obstacle is the same as doing so. </p>
  
  <p>thanx for helping.</p>