Excess code generated for lambdas

4 posts, 0 answers
  1. Craig
    Craig avatar
    2 posts
    Member since:
    Dec 2015

    Posted 09 Dec 2015 Link to this post

    I have the following line of code in my C# application:

    ArgumentHelpers.ThrowIfNullOrWhitespace(() => args[0]);

    JustDecompile shows the following decompilation:

    ArgumentHelpers.ThrowIfNullOrWhitespace(new Expression<Func<string>>[] { Expression.Lambda<Func<string>>(Expression.ArrayIndex(Expression.Field(Expression.Constant(variable, typeof(Program.<>c__DisplayClass0_0)), FieldInfo.GetFieldFromHandle(typeof(Program.<>c__DisplayClass0_0).GetField("args").FieldHandle)), Expression.Constant(0, typeof(int))), new ParameterExpression[0]) });

    ILSpy shows the following decompilation:

    ArgumentHelpers.ThrowIfNullOrWhitespace(new Expression<Func<string>>[]<br>{<br>    () => args[0]<br>});

    It would be nice if JustDecompile generated the more concise syntax.

  2. Alexander
    Admin
    Alexander avatar
    80 posts

    Posted 11 Dec 2015 Link to this post

    Hi,

    Thank you for letting us know about this problem.

    Would it be possible that you provide us with some more details about your code, such as the signature of the ThrowIfNullOrWhitespace method and the type of the args variable? By doing that you will enable us to reproduce exactly the problem you have.

    Thank you in advance.

    Regards,
    Alexander
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
  4. Craig
    Craig avatar
    2 posts
    Member since:
    Dec 2015

    Posted 11 Dec 2015 in reply to Alexander Link to this post

    I've been trying to get a copy of the assembly to Tsviatko Yovtchev. I think we may have finally succeeded using ftp. Here's the actual code, boiled down to a very simple p.o.c.

    using System;
    using System.Linq.Expressions;
     
    namespace ConsoleApplication5
    {
        public static class ArgumentHelpers
        {
            private static TObject GetReferenceValue<TObject>(Expression<Func<TObject>> reference)
            { return reference.Compile().Invoke(); }
     
            public static void ThrowIfNullOrWhitespace(params Expression<Func<string>>[] argumentSelector)
            {
                foreach (var selector in argumentSelector)
                {
                    if (String.IsNullOrWhiteSpace(GetReferenceValue(selector)))
                    { throw new ArgumentException("value cannot be null or whitespace", nameof(selector)); }
                }
            }
        }
    }
    namespace ConsoleApplication5
    {
        class Program
        {
            static void Main(string[] args)
            {
                ArgumentHelpers.ThrowIfNullOrWhitespace(() => args[0]);
            }
        }
    }

  5. Alexander
    Admin
    Alexander avatar
    80 posts

    Posted 11 Dec 2015 Link to this post

    Hi,

    Thank you for sending us the code. The bug is now reproduced and added to our backlog.

    Regards,
    Alexander
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top