Permalink
Browse files

Output constraints for generic methods.

  • Loading branch information...
1 parent ed118a1 commit c24ec99ae052fd84478333ee2affc46e14f91322 @dgrunwald dgrunwald committed Mar 3, 2011
Showing with 21 additions and 2 deletions.
  1. +17 −2 ICSharpCode.Decompiler/Ast/AstBuilder.cs
  2. +4 −0 ICSharpCode.Decompiler/Tests/Generics.cs
@@ -513,8 +513,23 @@ IEnumerable<TypeParameterDeclaration> MakeTypeParameters(IEnumerable<GenericPara
IEnumerable<Constraint> MakeConstraints(IEnumerable<GenericParameter> genericParameters)
{
- // TODO
- return Enumerable.Empty<Constraint>();
+ foreach (var gp in genericParameters) {
+ Constraint c = new Constraint();
+ c.TypeParameter = CleanName(gp.Name);
+ // class/struct must be first
+ if (gp.HasReferenceTypeConstraint)
+ c.BaseTypes.Add(new PrimitiveType("class"));
+ if (gp.HasNotNullableValueTypeConstraint)
+ c.BaseTypes.Add(new PrimitiveType("struct"));
+
+ foreach (var constraintType in gp.Constraints)
+ c.BaseTypes.Add(ConvertType(constraintType));
+
+ if (gp.HasDefaultConstructorConstraint)
+ c.BaseTypes.Add(new PrimitiveType("new")); // new() must be last
+ if (c.BaseTypes.Any())
+ yield return c;
+ }
}
ConstructorDeclaration CreateConstructor(MethodDefinition methodDef)
@@ -27,4 +27,8 @@ public void Grow(int capacity)
}
}
}
+
+ public static void MethodWithConstraint<T, S>() where T : class, S where S : ICloneable, new()
+ {
+ }
}

0 comments on commit c24ec99

Please sign in to comment.