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;
}