This is a migrated thread and some comments may be shown as answers.

Excess code generated for lambdas

3 Answers 17 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Craig
Top achievements
Rank 1
Craig asked on 09 Dec 2015, 04:45 PM

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.

3 Answers, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 11 Dec 2015, 09:26 AM
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
0
Craig
Top achievements
Rank 1
answered on 11 Dec 2015, 03:10 PM

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]);
        }
    }
}

0
Alexander
Telerik team
answered on 11 Dec 2015, 05:24 PM
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
Tags
General Discussions
Asked by
Craig
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Craig
Top achievements
Rank 1
Share this question
or