Browse Source

partial support for named arguments in attributes.

pull/52/head
Artur Zgodziski 15 years ago
parent
commit
09177affc1
  1. 31
      ICSharpCode.Decompiler/Ast/AstBuilder.cs
  2. 10
      ICSharpCode.Decompiler/Tests/CustomAttributes/CustomAttributeSamples.cs

31
ICSharpCode.Decompiler/Ast/AstBuilder.cs

@ -600,21 +600,30 @@ namespace Decompiler
attribute.Type = ConvertType(customAttribute.AttributeType); attribute.Type = ConvertType(customAttribute.AttributeType);
section.Attributes.Add(attribute); section.Attributes.Add(attribute);
foreach (var parameter in customAttribute.ConstructorArguments) if(customAttribute.HasConstructorArguments)
{ foreach (var parameter in customAttribute.ConstructorArguments)
var isEnum = parameter.Type.IsValueType && !parameter.Type.IsPrimitive;
Expression parameterValue;
if (isEnum)
{ {
parameterValue = MakePrimitive(Convert.ToInt64(parameter.Value), parameter.Type); var isEnum = parameter.Type.IsValueType && !parameter.Type.IsPrimitive;
Expression parameterValue;
if (isEnum)
{
parameterValue = MakePrimitive(Convert.ToInt64(parameter.Value), parameter.Type);
}
else
{
parameterValue = new PrimitiveExpression(parameter.Value);
}
attribute.Arguments.Add(parameterValue);
} }
else
if (customAttribute.HasProperties)
foreach (var property in customAttribute.Properties)
{ {
parameterValue = new PrimitiveExpression(parameter.Value); var propertyReference = customAttribute.AttributeType.Resolve().Properties.First(pr => pr.Name == property.Name);
var propertyName = new IdentifierExpression(property.Name).WithAnnotation(propertyReference);
var propertyArgument = new PrimitiveExpression(property.Argument.Value);
attribute.Arguments.Add(new AssignmentExpression(propertyName, propertyArgument));
} }
attribute.Arguments.Add(parameterValue);
}
} }
attributedNode.Attributes.Add(section); attributedNode.Attributes.Add(section);

10
ICSharpCode.Decompiler/Tests/CustomAttributes/CustomAttributeSamples.cs

@ -11,7 +11,7 @@ namespace ParameterLessAttributeUsage
[Flags] [Flags]
public enum EnumWithFlagsAttribute public enum EnumWithFlagsAttribute
{ {
None None = 0
} }
} }
//$$ AttributeWithEnumArgument //$$ AttributeWithEnumArgument
@ -79,3 +79,11 @@ namespace AttributeAppliedToMethod
} }
} }
} }
//$$ NamedParameter
namespace NamedParameter
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class MyAttributeAttribute : Attribute
{
}
}

Loading…
Cancel
Save