Skip to content

Commit

Permalink
Remove "Attribute" suffix and add support for attributes on type para…
Browse files Browse the repository at this point in the history
…meters.
  • Loading branch information
dgrunwald committed Mar 3, 2011
1 parent d1ccd69 commit ed118a1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
20 changes: 15 additions & 5 deletions ICSharpCode.Decompiler/Ast/AstBuilder.cs
Expand Up @@ -499,11 +499,16 @@ MethodDeclaration CreateMethod(MethodDefinition methodDef)

IEnumerable<TypeParameterDeclaration> MakeTypeParameters(IEnumerable<GenericParameter> genericParameters)
{
return genericParameters.Select(
gp => new TypeParameterDeclaration {
Name = CleanName(gp.Name),
Variance = gp.IsContravariant ? VarianceModifier.Contravariant : gp.IsCovariant ? VarianceModifier.Covariant : VarianceModifier.Invariant
});
foreach (var gp in genericParameters) {
TypeParameterDeclaration tp = new TypeParameterDeclaration();
tp.Name = CleanName(gp.Name);
if (gp.IsContravariant)
tp.Variance = VarianceModifier.Contravariant;
else if (gp.IsCovariant)
tp.Variance = VarianceModifier.Covariant;
ConvertCustomAttributes(tp, gp);
yield return tp;
}
}

IEnumerable<Constraint> MakeConstraints(IEnumerable<GenericParameter> genericParameters)
Expand Down Expand Up @@ -616,6 +621,11 @@ static void ConvertCustomAttributes(AstNode attributedNode, ICustomAttributeProv
var attribute = new ICSharpCode.NRefactory.CSharp.Attribute();
attribute.Type = ConvertType(customAttribute.AttributeType);
attributes.Add(attribute);

SimpleType st = attribute.Type as SimpleType;
if (st != null && st.Identifier.EndsWith("Attribute", StringComparison.Ordinal)) {
st.Identifier = st.Identifier.Substring(0, st.Identifier.Length - "Attribute".Length);
}

if(customAttribute.HasConstructorArguments) {
foreach (var parameter in customAttribute.ConstructorArguments) {
Expand Down
Expand Up @@ -382,7 +382,7 @@ public class MyAttributeAttribute : Attribute
}
public class MyClass
{
public string this[[MyAttribute(Field = 2)]int index1, [MyAttribute(Field = 3)]int index2]
public string this[[MyAttribute(Field = 2)] int index1, [MyAttribute(Field = 3)] int index2]
{
get
{
Expand All @@ -396,3 +396,14 @@ public class MyClass
}
}
}
//$$ ClassAttributeOnTypeParameter
namespace ClassAttributeOnTypeParameter
{
[AttributeUsage(AttributeTargets.All)]
public class MyAttributeAttribute : Attribute
{
}
public class MyClass<[MyAttribute] T>
{
}
}
Expand Up @@ -14,7 +14,7 @@ public override object VisitAttribute(NRefactory.CSharp.Attribute attribute, obj
var section = (AttributeSection)attribute.Parent;
SimpleType type = attribute.Type as SimpleType;
if (section.AttributeTarget == AttributeTarget.Assembly &&
(type.Identifier == "CompilationRelaxationsAttribute" || type.Identifier == "RuntimeCompatibilityAttribute"))
(type.Identifier == "CompilationRelaxations" || type.Identifier == "RuntimeCompatibility"))
{
attribute.Remove();
if (section.Attributes.Count == 0)
Expand Down
Expand Up @@ -1135,7 +1135,9 @@ public object VisitAttributeSection(AttributeSection attributeSection, object da
}
WriteCommaSeparatedList(attributeSection.Attributes.SafeCast<Attribute, AstNode>());
WriteToken("]", AstNode.Roles.RBracket);
if (!(attributeSection.Parent is ParameterDeclaration))
if (attributeSection.Parent is ParameterDeclaration || attributeSection.Parent is TypeParameterDeclaration)
Space();
else
NewLine();
return EndNode(attributeSection);
}
Expand Down

0 comments on commit ed118a1

Please sign in to comment.