Problems Decompiling C# Conditional Operator

2 posts, 0 answers
  1. Dave
    Dave avatar
    5 posts
    Member since:
    Aug 2010

    Posted 24 May 2011 Link to this post

    JustDecompile produces invalid C# when the original code uses C#'s conditional operator (?:)

    Sample Code:
    public void SampleMethod(int arg)
    {
        var isEven = arg % 2 == 0;
        var val = isEven ? SampleEnum.Item1 : SampleEnum.Item2;
    }

    Result:
    public void SampleMethod(int arg)
    {
        bool flag = (arg % 2) == 0;
        SampleEnum sampleEnum = flag || SampleEnum.Item2;
    }

    The code produced by JustDecompile will not compile and results in the error: error CS0019: Operator '||' cannot be applied to operands of type 'bool' and 'JustDecompileTests.SampleEnum'

    If the condition is inline with the conditional operator the result is even worse.

    Sample Code:
    public void SampleMethod(int arg)
    {
        var val = arg % 2 == 0 ? SampleEnum.Item1 : SampleEnum.Item2;
    }

    Result:
    public void SampleMethod(int arg)
    {
        SampleEnum sampleEnum = !arg % SampleEnum.Item2 || SampleEnum.Item2;
    }

    In addition to the invalid || operator we also have an invalid negation of an int and an invalid modulus of an int and an enum.
  2. Chris
    Admin
    Chris avatar
    221 posts

    Posted 27 May 2011 Link to this post

    Hi Dave,
    Again, thanks a lot for the detailed info. We've reproduced all these cases and we've logged them for fixing. The fix will be available either for the Q2 release or in some of the next internal builds so please, stay tuned.
    Your Telerik points have been updated. Once again, thanks for the feedback!

    Regards,
    Chris
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  3. DevCraft banner
Back to Top