Copy a type reference used by an iterator method to a type reference used by the iterator closure. That is, replace occurences of the generic method parameters with corresponding generic type parameters.
Inheritance: MethodBodyMappingMutator
    private NestedTypeDefinition CreateClosureClass(bool makeGeneric) {
      CustomAttribute compilerGeneratedAttribute = new CustomAttribute();
      compilerGeneratedAttribute.Constructor = this.CompilerGeneratedCtor;

      NestedTypeDefinition result = new NestedTypeDefinition();
      string signature = MemberHelper.GetMethodSignature(this.method, NameFormattingOptions.Signature | NameFormattingOptions.ReturnType | NameFormattingOptions.TypeParameters);
      //result.Name = this.host.NameTable.GetNameFor("closureclass");
      result.Name = this.host.NameTable.GetNameFor(signature + " closure " + this.counter);
      this.counter++;
      result.Attributes = new List<ICustomAttribute>(1) { compilerGeneratedAttribute };
      result.BaseClasses = new List<ITypeReference>(1) { this.host.PlatformType.SystemObject };
      result.ContainingTypeDefinition = this.generatedclosureClass != null ? this.generatedclosureClass : method.ContainingTypeDefinition;
      result.Fields = new List<IFieldDefinition>();
      if (makeGeneric) result.GenericParameters = new List<IGenericTypeParameter>();
      result.Methods = new List<IMethodDefinition>();
      result.InternFactory = this.host.InternFactory;
      result.IsBeforeFieldInit = true;
      result.IsClass = true;
      result.IsSealed = true;
      result.Layout = LayoutKind.Auto;
      result.StringFormat = StringFormatKind.Ansi;
      result.Visibility = TypeMemberVisibility.Private;

      //BoundField/*?*/ capturedThis;
      //var thisTypeReference = TypeDefinition.SelfInstance(this.method.ContainingTypeDefinition, this.host.InternFactory);
      //if (this.closureLocals.Count == 0 && this.FieldForCapturedLocalOrParameter.TryGetValue(thisTypeReference.InternedKey, out capturedThis)) {
      //  result.Fields.Add(capturedThis.Field);
      //  capturedThis.Field.ContainingTypeDefinition = result;
      //  capturedThis.Field.Type = this.Visit(capturedThis.Field.Type);
      //}

      if (makeGeneric) {
        List<IGenericMethodParameter> genericMethodParameters = new List<IGenericMethodParameter>();
        ushort count = 0;
        if (this.method.IsGeneric) {
          foreach (var genericMethodParameter in this.method.GenericParameters) {
            genericMethodParameters.Add(genericMethodParameter);
            GenericTypeParameter newTypeParam = new GenericTypeParameter() {
              Name = this.host.NameTable.GetNameFor(genericMethodParameter.Name.Value + "_"),
              Index = (count++),
              InternFactory = this.host.InternFactory,
              PlatformType = this.host.PlatformType,
            };
            this.genericTypeParameterMapping[genericMethodParameter.InternedKey] = newTypeParam;
            newTypeParam.DefiningType = result;
            result.GenericParameters.Add(newTypeParam);
          }
        }
        this.copyTypeToClosure = new CopyTypeFromIteratorToClosure(this.host, genericTypeParameterMapping);
        if (this.method.IsGeneric) {
          // Duplicate Constraints
          foreach (var genericMethodParameter in genericMethodParameters) {
            GenericTypeParameter correspondingTypeParameter = (GenericTypeParameter)this.genericTypeParameterMapping[genericMethodParameter.InternedKey];
            if (genericMethodParameter.Constraints != null) {
              correspondingTypeParameter.Constraints = new List<ITypeReference>();
              foreach (ITypeReference t in genericMethodParameter.Constraints) {
                correspondingTypeParameter.Constraints.Add(copyTypeToClosure.Visit(t));
              }
            }
          }
        }
      }

      this.generatedclosureClass = result;
      classList.Add(result);
      return result;
    }
        private NestedTypeDefinition CreateClosureClass(bool makeGeneric)
        {
            CustomAttribute compilerGeneratedAttribute = new CustomAttribute();

            compilerGeneratedAttribute.Constructor = this.CompilerGeneratedCtor;

            NestedTypeDefinition result = new NestedTypeDefinition();
            string signature            = MemberHelper.GetMethodSignature(this.method, NameFormattingOptions.Signature | NameFormattingOptions.ReturnType | NameFormattingOptions.TypeParameters);

            //result.Name = this.host.NameTable.GetNameFor("closureclass");
            result.Name = this.host.NameTable.GetNameFor(signature + " closure " + this.counter);
            this.counter++;
            result.Attributes = new List <ICustomAttribute>(1)
            {
                compilerGeneratedAttribute
            };
            result.BaseClasses = new List <ITypeReference>(1)
            {
                this.host.PlatformType.SystemObject
            };
            result.ContainingTypeDefinition = this.generatedclosureClass != null ? this.generatedclosureClass : method.ContainingTypeDefinition;
            result.Fields = new List <IFieldDefinition>();
            if (makeGeneric)
            {
                result.GenericParameters = new List <IGenericTypeParameter>();
            }
            result.Methods           = new List <IMethodDefinition>();
            result.InternFactory     = this.host.InternFactory;
            result.IsBeforeFieldInit = true;
            result.IsClass           = true;
            result.IsSealed          = true;
            result.Layout            = LayoutKind.Auto;
            result.StringFormat      = StringFormatKind.Ansi;
            result.Visibility        = TypeMemberVisibility.Private;

            //BoundField/*?*/ capturedThis;
            //var thisTypeReference = TypeDefinition.SelfInstance(this.method.ContainingTypeDefinition, this.host.InternFactory);
            //if (this.closureLocals.Count == 0 && this.FieldForCapturedLocalOrParameter.TryGetValue(thisTypeReference.InternedKey, out capturedThis)) {
            //  result.Fields.Add(capturedThis.Field);
            //  capturedThis.Field.ContainingTypeDefinition = result;
            //  capturedThis.Field.Type = this.Visit(capturedThis.Field.Type);
            //}

            if (makeGeneric)
            {
                List <IGenericMethodParameter> genericMethodParameters = new List <IGenericMethodParameter>();
                ushort count = 0;
                if (this.method.IsGeneric)
                {
                    foreach (var genericMethodParameter in this.method.GenericParameters)
                    {
                        genericMethodParameters.Add(genericMethodParameter);
                        GenericTypeParameter newTypeParam = new GenericTypeParameter()
                        {
                            Name          = this.host.NameTable.GetNameFor(genericMethodParameter.Name.Value + "_"),
                            Index         = (count++),
                            InternFactory = this.host.InternFactory,
                            PlatformType  = this.host.PlatformType,
                        };
                        this.genericTypeParameterMapping[genericMethodParameter.InternedKey] = newTypeParam;
                        newTypeParam.DefiningType = result;
                        result.GenericParameters.Add(newTypeParam);
                    }
                }
                this.copyTypeToClosure = new CopyTypeFromIteratorToClosure(this.host, genericTypeParameterMapping);
                if (this.method.IsGeneric)
                {
                    // Duplicate Constraints
                    foreach (var genericMethodParameter in genericMethodParameters)
                    {
                        GenericTypeParameter correspondingTypeParameter = (GenericTypeParameter)this.genericTypeParameterMapping[genericMethodParameter.InternedKey];
                        if (genericMethodParameter.Constraints != null)
                        {
                            correspondingTypeParameter.Constraints = new List <ITypeReference>();
                            foreach (ITypeReference t in genericMethodParameter.Constraints)
                            {
                                correspondingTypeParameter.Constraints.Add(copyTypeToClosure.Visit(t));
                            }
                        }
                    }
                }
            }

            this.generatedclosureClass = result;
            classList.Add(result);
            return(result);
        }
    /// <summary>
    /// Create the iterator closure class and add it to the private helper types list. 
    /// </summary>
    private IteratorClosureInformation CreateIteratorClosure(BlockStatement blockStatement) {
      IteratorClosureInformation result = new IteratorClosureInformation(this.host);
      CustomAttribute compilerGeneratedAttribute = new CustomAttribute();
      compilerGeneratedAttribute.Constructor = this.CompilerGeneratedCtor;

      // Create the closure class with CompilerGeneratedAttribute, the list of generic type parameters isomorphic to 
      // those of the iterator method. 
      NestedTypeDefinition iteratorClosureType = new NestedTypeDefinition() {
        ExplicitImplementationOverrides = new List<IMethodImplementation>(),
        Fields = new List<IFieldDefinition>(),
        GenericParameters = new List<IGenericTypeParameter>(),
        Interfaces = new List<ITypeReference>(),
        Methods = new List<IMethodDefinition>(),
        NestedTypes = new List<INestedTypeDefinition>(),
      };
      this.privateHelperTypes.Add(iteratorClosureType);
      result.ClosureDefinition = iteratorClosureType;
      List<IGenericMethodParameter> genericMethodParameters = new List<IGenericMethodParameter>();
      ushort count = 0;
      foreach (var genericMethodParameter in method.GenericParameters) {
        genericMethodParameters.Add(genericMethodParameter);
        GenericTypeParameter newTypeParam = new GenericTypeParameter() {
          Name = this.host.NameTable.GetNameFor(genericMethodParameter.Name.Value + "_"),
          InternFactory = this.host.InternFactory,
          PlatformType = this.host.PlatformType,
          Index = (count++),
        };
        this.genericTypeParameterMapping[genericMethodParameter.InternedKey] = newTypeParam;
        newTypeParam.DefiningType = iteratorClosureType;
        iteratorClosureType.GenericParameters.Add(newTypeParam);
      }

      this.copyTypeToClosure = new CopyTypeFromIteratorToClosure(this.host, this.genericTypeParameterMapping);

      // Duplicate Constraints
      foreach (var genericMethodParameter in genericMethodParameters) {
        GenericTypeParameter correspondingTypeParameter = (GenericTypeParameter)this.genericTypeParameterMapping[genericMethodParameter.InternedKey];
        if (genericMethodParameter.Constraints != null) {
          correspondingTypeParameter.Constraints = new List<ITypeReference>();
          foreach (ITypeReference t in genericMethodParameter.Constraints) {
            correspondingTypeParameter.Constraints.Add(copyTypeToClosure.Visit(t));
          }
        }
      }
      // elementTypes contains only one element, the argument type of the return type (the T in IEnumerable<T>) of the iterator method, or simply System.Object if the
      // iterator is not generic. 
      IEnumerable<ITypeReference> elementTypes = GetClosureEnumeratorTypeArguments(method.Type);
      // elementType of the IEnumerable. 
      ITypeReference elementType = null;
      foreach (ITypeReference t in elementTypes) {
        elementType = t; break;
      }
      result.ElementType = this.copyTypeToClosure.Visit(elementType);

      // Set up the iterator closure class. 
      // TODO: name generation to follow the convention of csc. 
      iteratorClosureType.Name = this.host.NameTable.GetNameFor("<" + this.method.Name.Value + ">" + "ic__" + this.privateHelperTypes.Count);
      iteratorClosureType.Attributes = new List<ICustomAttribute>(1) { compilerGeneratedAttribute };
      iteratorClosureType.BaseClasses = new List<ITypeReference>(1) { this.host.PlatformType.SystemObject };
      iteratorClosureType.ContainingTypeDefinition = this.method.ContainingTypeDefinition;
      iteratorClosureType.ExplicitImplementationOverrides = new List<IMethodImplementation>(7);
      iteratorClosureType.InternFactory = this.host.InternFactory;
      iteratorClosureType.IsBeforeFieldInit = true;
      iteratorClosureType.IsClass = true;
      iteratorClosureType.IsSealed = true;
      iteratorClosureType.Layout = LayoutKind.Auto;
      iteratorClosureType.StringFormat = StringFormatKind.Ansi;
      iteratorClosureType.Visibility = TypeMemberVisibility.Private;

      /* Interfaces. */
      result.InitializeInterfaces(result.ElementType, this.isEnumerable);

      /* Fields, Methods, and Properties. */
      CreateIteratorClosureFields(result);
      CreateIteratorClosureConstructor(result);
      CreateIteratorClosureMethods(result, blockStatement);
      CreateIteratorClosureProperties(result);
      return result;
    }