Browse Source

Fix decompilation of arrays as attribute arguments. Closes #82.

pull/100/head
Daniel Grunwald 14 years ago
parent
commit
c4335dd2ef
  1. 26
      ICSharpCode.Decompiler/Ast/AstBuilder.cs
  2. 26
      ICSharpCode.Decompiler/Tests/CustomAttributes/S_CustomAttributes.cs

26
ICSharpCode.Decompiler/Ast/AstBuilder.cs

@ -1074,17 +1074,31 @@ namespace ICSharpCode.Decompiler.Ast
} }
} }
private static Expression ConvertArgumentValue(CustomAttributeArgument parameter) private static Expression ConvertArgumentValue(CustomAttributeArgument argument)
{ {
var type = parameter.Type.Resolve(); if (argument.Value is CustomAttributeArgument[]) {
ArrayInitializerExpression arrayInit = new ArrayInitializerExpression();
foreach (CustomAttributeArgument element in (CustomAttributeArgument[])argument.Value) {
arrayInit.Elements.Add(ConvertArgumentValue(element));
}
ArrayType arrayType = argument.Type as ArrayType;
return new ArrayCreateExpression {
Type = ConvertType(arrayType != null ? arrayType.ElementType : argument.Type),
Initializer = arrayInit
};
} else if (argument.Value is CustomAttributeArgument) {
// occurs with boxed arguments
return ConvertArgumentValue((CustomAttributeArgument)argument.Value);
}
var type = argument.Type.Resolve();
if (type != null && type.IsEnum) { if (type != null && type.IsEnum) {
return MakePrimitive(Convert.ToInt64(parameter.Value), type); return MakePrimitive(Convert.ToInt64(argument.Value), type);
} else if (parameter.Value is TypeReference) { } else if (argument.Value is TypeReference) {
return new TypeOfExpression() { return new TypeOfExpression() {
Type = ConvertType((TypeReference)parameter.Value), Type = ConvertType((TypeReference)argument.Value),
}; };
} else { } else {
return new PrimitiveExpression(parameter.Value); return new PrimitiveExpression(argument.Value);
} }
} }
#endregion #endregion

26
ICSharpCode.Decompiler/Tests/CustomAttributes/S_CustomAttributes.cs

@ -5,7 +5,7 @@ using System;
namespace aa namespace aa
{ {
public static class CustomAtributes public static class CustomAttributes
{ {
[Flags] [Flags]
public enum EnumWithFlag public enum EnumWithFlag
@ -20,13 +20,13 @@ namespace aa
[AttributeUsage(AttributeTargets.All)] [AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute public class MyAttribute : Attribute
{ {
public MyAttribute(CustomAtributes.EnumWithFlag en) public MyAttribute(object val)
{ {
} }
} }
[CustomAtributes.MyAttribute(CustomAtributes.EnumWithFlag.Item1 | CustomAtributes.EnumWithFlag.Item2)] [CustomAttributes.MyAttribute(CustomAttributes.EnumWithFlag.Item1 | CustomAttributes.EnumWithFlag.Item2)]
private static int field; private static int field;
[CustomAtributes.MyAttribute(CustomAtributes.EnumWithFlag.All)] [CustomAttributes.MyAttribute(CustomAttributes.EnumWithFlag.All)]
public static string Property public static string Property
{ {
get get
@ -42,5 +42,23 @@ namespace aa
AttributeTargets attributeTargets = AttributeTargets.Property | AttributeTargets.Field; AttributeTargets attributeTargets = AttributeTargets.Property | AttributeTargets.Field;
Console.WriteLine("{0} $$$ {1}", AttributeTargets.Interface, attributeTargets); Console.WriteLine("{0} $$$ {1}", AttributeTargets.Interface, attributeTargets);
} }
// No Boxing
[CustomAttributes.MyAttribute(new StringComparison[]
{
StringComparison.Ordinal,
StringComparison.CurrentCulture
})]
public static void ArrayAsAttribute1()
{
}
// Boxing of each array element
[CustomAttributes.MyAttribute(new object[]
{
StringComparison.Ordinal,
StringComparison.CurrentCulture
})]
public static void ArrayAsAttribute2()
{
}
} }
} }
Loading…
Cancel
Save