Browse Source

corrected printing values of named parameters of attributes.

pull/52/head
Artur Zgodziski 15 years ago
parent
commit
1df7e5f777
  1. 42
      ICSharpCode.Decompiler/Ast/AstBuilder.cs
  2. 73
      ICSharpCode.Decompiler/Tests/CustomAttributes/CustomAttributeSamples.cs

42
ICSharpCode.Decompiler/Ast/AstBuilder.cs

@ -603,11 +603,31 @@ namespace Decompiler
if(customAttribute.HasConstructorArguments) if(customAttribute.HasConstructorArguments)
foreach (var parameter in customAttribute.ConstructorArguments) foreach (var parameter in customAttribute.ConstructorArguments)
{ {
var isEnum = parameter.Type.IsValueType && !parameter.Type.IsPrimitive; Expression parameterValue = ConvertArgumentValue(parameter);
attribute.Arguments.Add(parameterValue);
}
if (customAttribute.HasProperties)
foreach (var property in customAttribute.Properties)
{
var propertyReference = customAttribute.AttributeType.Resolve().Properties.First(pr => pr.Name == property.Name);
var propertyName = new IdentifierExpression(property.Name).WithAnnotation(propertyReference);
var argumentValue = ConvertArgumentValue(property.Argument);
attribute.Arguments.Add(new AssignmentExpression(propertyName, argumentValue));
}
}
attributedNode.Attributes.Add(section);
}
}
private static Expression ConvertArgumentValue(CustomAttributeArgument parameter)
{
var type = parameter.Type.Resolve();
Expression parameterValue; Expression parameterValue;
if (isEnum) if (type.IsEnum)
{ {
parameterValue = MakePrimitive(Convert.ToInt64(parameter.Value), parameter.Type); parameterValue = MakePrimitive(Convert.ToInt64(parameter.Value), type);
} }
else if (parameter.Value is TypeReference) else if (parameter.Value is TypeReference)
{ {
@ -620,21 +640,7 @@ namespace Decompiler
{ {
parameterValue = new PrimitiveExpression(parameter.Value); parameterValue = new PrimitiveExpression(parameter.Value);
} }
attribute.Arguments.Add(parameterValue); return parameterValue;
}
if (customAttribute.HasProperties)
foreach (var property in customAttribute.Properties)
{
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));
}
}
attributedNode.Attributes.Add(section);
}
} }

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

@ -124,11 +124,80 @@ public struct AttributeAppliedToStruct
{ {
public int Field; public int Field;
} }
//$$ NamedParameter //$$ NamedPropertyParameter
namespace NamedParameter namespace NamedPropertyParameter
{ {
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class MyAttributeAttribute : Attribute public class MyAttributeAttribute : Attribute
{ {
} }
} }
//$$ NamedStringPropertyParameter
namespace NamedStringPropertyParameter
{
[AttributeUsage(AttributeTargets.All)]
public class MyAttributeAttribute : Attribute
{
public string Prop
{
get
{
return "";
}
set
{
return;
}
}
}
[MyAttribute(Prop = "value")]
public class MyClass
{
}
}
//$$ NamedTypePropertyParameter
namespace NamedTypePropertyParameter
{
[AttributeUsage(AttributeTargets.All)]
public class MyAttributeAttribute : Attribute
{
public Type Prop
{
get
{
return null;
}
set
{
return;
}
}
}
[MyAttribute(Prop = typeof(Enum))]
public class MyClass
{
}
}
//$$ NamedEnumPropertyParameter
namespace NamedEnumPropertyParameter
{
[AttributeUsage(AttributeTargets.All)]
public class MyAttributeAttribute : Attribute
{
public AttributeTargets Prop
{
get
{
return AttributeTargets.All;
}
set
{
return;
}
}
}
[MyAttribute(Prop = (AttributeTargets.Class | AttributeTargets.Method))]
public class MyClass
{
}
}

Loading…
Cancel
Save