hazzik / DelegateDecompiler

A library which is able to decompile a delegate or a method body to its lambda representation
MIT License
526 stars 62 forks source link

Error decompiling unary operator Not for enum parameter #168

Closed billybraga closed 3 years ago

billybraga commented 3 years ago

Reproduced by adding this test in src/DelegateDecompiler.Tests/EnumTests.cs:

        [Test]
        public void TestNotEnumParameter()
        {
            Expression<Func<TestFlagEnum, TestFlagEnum>> expected = x => ~ x;
            Func<TestFlagEnum, TestFlagEnum> compiled = x => ~ x;
            Test(expected, compiled);
        }

Gives the following exception:

System.InvalidOperationException : The unary operator Not is not defined for the type 'DelegateDecompiler.Tests.EnumTests+TestFlagEnum'.
  Stack Trace:
     at System.Linq.Expressions.Expression.GetUserDefinedUnaryOperatorOrThrow(ExpressionType unaryType, String name, Expression operand)
   at System.Linq.Expressions.Expression.Not(Expression expression, MethodInfo method)
   at System.Linq.Expressions.Expression.Not(Expression expression)
   at DelegateDecompiler.Processor.Process() in /home/local/.../DelegateDecompiler/src/DelegateDecompiler/Processor.cs:line 441
   at DelegateDecompiler.Processor.Process(VariableInfo[] locals, IList`1 args, Instruction instruction, Type returnType) in /home/local/.../DelegateDecompiler/src/DelegateDecompiler/Processor.cs:line 102
   at DelegateDecompiler.MethodBodyDecompiler.Decompile(MethodInfo method, Type declaringType) in /home/local/.../DelegateDecompiler/src/DelegateDecompiler/MethodBodyDecompiler.cs:line 23
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at DelegateDecompiler.DecompileExtensions.Decompile(Delegate delegate) in /home/local/.../DelegateDecompiler/src/DelegateDecompiler/DecompileExtensions.cs:line 20
   at DelegateDecompiler.Tests.DecompilerTestsBase.Test[T](Expression`1 expected, T compiled) in /home/local/.../DelegateDecompiler/src/DelegateDecompiler.Tests/DecompilerTestsBase.cs:line 21

Also happens with more complex expressions.