private void AddGenericTypes(ClassDefinition cd, bool adaptInnerGenerics) { var collector = new GenericTypeCollector(this.CodeBuilder); collector.Process(cd); if (!adaptInnerGenerics) { return; } var counter = cd.GenericParameters.Count; var innerCollector = new DetectInnerGenerics(); cd.Accept(innerCollector); foreach (Node node in innerCollector.Values) { var param = (IGenericParameter)node.Entity; var gp = cd.GenericParameters.FirstOrDefault(gpd => gpd.Name.Equals(param.Name)); if (gp == null) { gp = CodeBuilder.CreateGenericParameterDeclaration(counter, param.Name); cd.GenericParameters.Add(gp); ++counter; } node.Entity = gp.Entity; gp["InternalGenericParent"] = (param as InternalGenericParameter).Node; } }
public void Process(ClassDefinition cd) { cd.Accept(this); var parameters = GenericParameters.ToArray(); for (var i = 0; i < parameters.Length; ++i) { var param = parameters[i]; var gen = cd.GenericParameters.FirstOrDefault(gp => gp.Name.Equals(param.Name)); var found = gen != null; if (!found) { gen = _codeBuilder.CreateGenericParameterDeclaration(i, param.Name); } foreach (IType baseType in param.GetTypeConstraints()) { gen.BaseTypes.Add(_codeBuilder.CreateTypeReference(baseType)); } if (param.MustHaveDefaultConstructor) { gen.Constraints |= GenericParameterConstraints.Constructable; } if (param.IsValueType) { gen.Constraints |= GenericParameterConstraints.ValueType; } else if (param.IsClass) { gen.Constraints |= GenericParameterConstraints.ReferenceType; } if (param.Variance == Variance.Covariant) { gen.Constraints |= GenericParameterConstraints.Covariant; } else if (param.Variance == Variance.Contravariant) { gen.Constraints |= GenericParameterConstraints.Contravariant; } if (!found) { cd.GenericParameters.Add(gen); } } }