diff --git a/ICSharpCode.Decompiler/Ast/AstBuilder.cs b/ICSharpCode.Decompiler/Ast/AstBuilder.cs index f39a4cbf0..05e82499f 100644 --- a/ICSharpCode.Decompiler/Ast/AstBuilder.cs +++ b/ICSharpCode.Decompiler/Ast/AstBuilder.cs @@ -1501,13 +1501,13 @@ namespace ICSharpCode.Decompiler.Ast { // cannot rely on type.IsValueType, it's not set for typerefs (but is set for typespecs) TypeDefinition enumDefinition = type.Resolve(); if (enumDefinition != null && enumDefinition.IsEnum) { + TypeCode enumBaseTypeCode = TypeCode.Int32; foreach (FieldDefinition field in enumDefinition.Fields) { if (field.IsStatic && object.Equals(CSharpPrimitiveCast.Cast(TypeCode.Int64, field.Constant, false), val)) - return ConvertType(enumDefinition).Member(field.Name).WithAnnotation(field); + return ConvertType(type).Member(field.Name).WithAnnotation(field); else if (!field.IsStatic && field.IsRuntimeSpecialName) - type = field.FieldType; // use primitive type of the enum + enumBaseTypeCode = TypeAnalysis.GetTypeCode(field.FieldType); // use primitive type of the enum } - TypeCode enumBaseTypeCode = TypeAnalysis.GetTypeCode(type); if (IsFlagsEnum(enumDefinition)) { long enumValue = val; Expression expr = null; @@ -1534,7 +1534,7 @@ namespace ICSharpCode.Decompiler.Ast continue; // skip None enum value if ((fieldValue & enumValue) == fieldValue) { - var fieldExpression = ConvertType(enumDefinition).Member(field.Name).WithAnnotation(field); + var fieldExpression = ConvertType(type).Member(field.Name).WithAnnotation(field); if (expr == null) expr = fieldExpression; else @@ -1543,7 +1543,7 @@ namespace ICSharpCode.Decompiler.Ast enumValue &= ~fieldValue; } if ((fieldValue & negatedEnumValue) == fieldValue) { - var fieldExpression = ConvertType(enumDefinition).Member(field.Name).WithAnnotation(field); + var fieldExpression = ConvertType(type).Member(field.Name).WithAnnotation(field); if (negatedExpr == null) negatedExpr = fieldExpression; else @@ -1561,7 +1561,7 @@ namespace ICSharpCode.Decompiler.Ast return new UnaryOperatorExpression(UnaryOperatorType.BitNot, negatedExpr); } } - return new Ast.PrimitiveExpression(CSharpPrimitiveCast.Cast(enumBaseTypeCode, val, false)).CastTo(ConvertType(enumDefinition)); + return new Ast.PrimitiveExpression(CSharpPrimitiveCast.Cast(enumBaseTypeCode, val, false)).CastTo(ConvertType(type)); } } TypeCode code = TypeAnalysis.GetTypeCode(type); diff --git a/ICSharpCode.Decompiler/Tests/Generics.cs b/ICSharpCode.Decompiler/Tests/Generics.cs index 9924583d6..9b7bd3fea 100644 --- a/ICSharpCode.Decompiler/Tests/Generics.cs +++ b/ICSharpCode.Decompiler/Tests/Generics.cs @@ -29,6 +29,12 @@ public static class Generics public Y Item2; } + public enum NestedEnum + { + A, + B + } + private T[] arr; public MyArray(int capacity) @@ -75,11 +81,13 @@ public static class Generics } } + private const Generics.MyArray.NestedEnum enumVal = Generics.MyArray.NestedEnum.A; private static Type type1 = typeof(List<>); private static Type type2 = typeof(Generics.MyArray<>); private static Type type3 = typeof(List<>.Enumerator); private static Type type4 = typeof(Generics.MyArray<>.NestedClass<>); private static Type type5 = typeof(List[]); + private static Type type6 = typeof(Generics.MyArray<>.NestedEnum); public static void MethodWithConstraint() where T : class, S where S : ICloneable, new() { diff --git a/ICSharpCode.Decompiler/Tests/TestRunner.cs b/ICSharpCode.Decompiler/Tests/TestRunner.cs index a05779ecc..52bef700c 100644 --- a/ICSharpCode.Decompiler/Tests/TestRunner.cs +++ b/ICSharpCode.Decompiler/Tests/TestRunner.cs @@ -158,7 +158,7 @@ namespace ICSharpCode.Decompiler.Tests { CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary { { "CompilerVersion", "v4.0" } }); CompilerParameters options = new CompilerParameters(); - options.CompilerOptions = "/unsafe"; + options.CompilerOptions = "/unsafe /o-"; options.ReferencedAssemblies.Add("System.Core.dll"); CompilerResults results = provider.CompileAssemblyFromSource(options, code); try {