0

All, I am trying to figure out how does lambda expression works in the complied time also in the run time. Say you have the source code like below. enter image description here

Currently, I tried to quick watch the variable. But unfortunately. Can not make it to view the source code of the Fun .Is there any other way to view what does actually code the Func<int> ageCalculator run?. Thanks.

Updated

No lucky things in the reflector kind tools. Please see it in the dotPeek. Thanks.

enter image description here

Updated 1

There are more items (Compiled generated class items) displayed in the tree when the option is enabled. But double-clicked these items. Just display the MyTempClass source code no new thing. What does it suppose to display ? Thanks.

enter image description here

Joe.wang
  • 11,537
  • 25
  • 103
  • 180

4 Answers4

1

You can't see the C# source code, because there is none. There's a class generated automatically by the compiler, so the only thing you could see is intermediate code (IL). That IL code might be displayed as C# by other tools like Reflector (I don't have such a tool integrated in Visual Studio, so I can't try).

You can see it in dotPeek when you enable "Show compiler generated code":

dotPeek Setting

Next, right click and choose "Decompiled sources" to show the generated code:

dotPeek Showing the generated class

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
1

The key issue is that by returning a Func you return a compiled lambda, you want to return an Expression<Func<int>> instead. You can then call ToString() to see its representation and Compile().Invoke() to run it

Expression<Func<int>> AgeCalculator() {
  int myAge = 30;
  return () => myAge;
}

public void Closure() {
  var ageCalculator = AgeCalculator();
  Console.WriteLine(ageCalculator.ToString());
  Console.WriteLine(ageCalculator.Compile().Invoke());
}
Bob Vale
  • 18,094
  • 1
  • 42
  • 49
  • Got it . It sounds like a new lambda expression with generated class code display . It looks like `()=>value(xxxnamespace.xxxtestclass+()c_DisplayClass1_0.myAge)`. What does it mean `value`? I didn't found it in the MSDN. Thanks. – Joe.wang Jun 27 '17 at 11:17
  • I meant what does `ageCalculator.ToString()` display in the `Console`. – Joe.wang Jun 27 '17 at 11:19
  • 1
    `value(xxxnamespace.xxxtestclass+()c_DisplayClass1_0.myAg‌​e)` returns the value of the captured context variable myAge – Bob Vale Jun 27 '17 at 14:37
  • @Joe.wang the ToString() will return the string representation of the lambda. If you are debugging then you can inspect the lambda – Bob Vale Jun 27 '17 at 14:38
  • Once you have the expression instead of just a func, you could use the accepted answer to this question [convert-lambdaexpression-to-string-including-values](https://stackoverflow.com/questions/15478711/convert-lambdaexpression-to-string-including-values) to output a more useful string – Bob Vale Jun 27 '17 at 14:42
  • helpful link. Thanks. @BobVale – Joe.wang Jul 12 '17 at 09:36
0

I can see some stuff not entirely being used as it is intended to. Please consider the code below:

class Program
{
    static void Main(string[] args)
    {
        // You do not call the method to assign it to the variable, 
        // you point to the method (without parentheses)
        Func<int> answer = GetTheAnswerToEverything;

        // Here you actually call the method
        Console.WriteLine(answer());
        Console.ReadLine();
    }

    // This is the method that you call when you write **add()**
    private static int GetTheAnswerToEverything() => 42;
}

In this example, you actually call the GetTheAnswerToEverything method when you invoke answer().

For more information, see Func Delegate

Encapsulates a method that has no parameters and returns a value of the type specified by the TResult parameter.

rickvdbosch
  • 14,105
  • 2
  • 40
  • 53
  • I tried to print it in the `Closure()` like `Console.WriteLine(ageCalculator.ToString());`. But only got info like `System.Func ~1[System.Int32]` . – Joe.wang Jun 27 '17 at 09:43
  • That's because you're not _calling_ the method. You're pointing at it. If you want to print the result, call the method ```Console.WriteLine(ageCalculator());``` – rickvdbosch Jun 27 '17 at 09:46
0

I'm not sure if this it's what you want, but LinqPad has some views Tree, IL that maybe it's what you're looking for....

enter image description here enter image description here

jjchiw
  • 4,375
  • 1
  • 29
  • 30
  • Thanks, Can LinqPad view the detail of lambda (such as source code or generated class code)from the assembly dll? – Joe.wang Jun 27 '17 at 10:14
  • BTW. Isn't it better to view the actually running source code when deduging the lambda expression. Right? – Joe.wang Jun 27 '17 at 10:17
  • the IL tab is the generated code.... I don't know what are you expecting to see about the lambda expression, maybe reading the sourcecode https://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,6dbbba50dd6655cd – jjchiw Jun 27 '17 at 10:38