Browse Source

corrected printing values of named parameters of attributes.

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

44
ICSharpCode.Decompiler/Ast/AstBuilder.cs

@ -603,23 +603,7 @@ namespace Decompiler @@ -603,23 +603,7 @@ namespace Decompiler
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);
}
else if (parameter.Value is TypeReference)
{
parameterValue = new TypeOfExpression()
{
Type = ConvertType((TypeReference)parameter.Value),
};
}
else
{
parameterValue = new PrimitiveExpression(parameter.Value);
}
Expression parameterValue = ConvertArgumentValue(parameter);
attribute.Arguments.Add(parameterValue);
}
@ -628,8 +612,8 @@ namespace Decompiler @@ -628,8 +612,8 @@ namespace Decompiler
{
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));
var argumentValue = ConvertArgumentValue(property.Argument);
attribute.Arguments.Add(new AssignmentExpression(propertyName, argumentValue));
}
}
@ -637,6 +621,28 @@ namespace Decompiler @@ -637,6 +621,28 @@ namespace Decompiler
}
}
private static Expression ConvertArgumentValue(CustomAttributeArgument parameter)
{
var type = parameter.Type.Resolve();
Expression parameterValue;
if (type.IsEnum)
{
parameterValue = MakePrimitive(Convert.ToInt64(parameter.Value), type);
}
else if (parameter.Value is TypeReference)
{
parameterValue = new TypeOfExpression()
{
Type = ConvertType((TypeReference)parameter.Value),
};
}
else
{
parameterValue = new PrimitiveExpression(parameter.Value);
}
return parameterValue;
}
internal static Expression MakePrimitive(long val, TypeReference type)
{

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

@ -124,11 +124,80 @@ public struct AttributeAppliedToStruct @@ -124,11 +124,80 @@ public struct AttributeAppliedToStruct
{
public int Field;
}
//$$ NamedParameter
namespace NamedParameter
//$$ NamedPropertyParameter
namespace NamedPropertyParameter
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
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