Cannot access to property of anonymous type in debug mode (VS2013)
Asked Answered
F

1

6

In this sample console app:

class Program
{
    static void Main()
    {
        DoAsyncFoo();
        Console.ReadKey();
    }

    private static async void DoAsyncFoo()
    {
        var task = CollectStatsAsync();
        dynamic foo = await task;
        Console.WriteLine(foo.NumberOfCores);
    }

    private static async Task<dynamic> CollectStatsAsync()
    {
        return CollectStats();
    }

    private static dynamic CollectStats()
    {
        return new { NumberOfCores = 3 };
    }
}

When I put breakpoint to

Console.WriteLine(foo.NumberOfCores)

and evaluate foo.NumberOfCores in debug mode, the output of the error is:

collectedStats.NumberOfCores 'object' does not contain a definition for 'NumberOfCores' and no extension method 'NumberOfCores' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

Because collectedStats is "Anonymous Object", not "dynamic". However, the function returns dynamic, and I assigned it as dynamic.

Evaluation is successfull for:

((dynamic)foo).NumberOfCores;

By the way, I have realized that if I write the function synchronously, debugger can directly return the result. So it should be about async.

Note: And I have also tried returning Expando Object instead of Anonymous Type from function, the result is same.

Freer answered 16/9, 2015 at 14:54 Comment(7)
@AlexeiLevenkov it is not in another assembly.Freer
@CodeCaster no, I already tried returning the expando object and I will attach it to the question.Freer
@Freer ok. Now it is clear that dynamic does not contain a definition for a property from a project reference is not duplicate due to code being in the same assembly.Codify
Cannot reproduce. Please create a minimal reproducible example. Do you perform this call over WCF or anything (which does mean the CollectStats implementation is in another assembly)?Stavanger
@Stavanger I have realized that it only happens in debug mode, then I've edited my question. Here is the sample code ideone.com/CLpHa9, If you put breakpoint to line 19, and evaluate foo.NumberOfCores in VS2013 QuickWatch screen, you will see that.Freer
The debugger throws your exception, while the Console.WriteLine() does print 3... Weird behavior. Perhaps reduce your question to show the latest code that reproduces it, for brevity? :-)Stavanger
@Stavanger I've just simplified it :)Freer
F
0

FYI I just tested this in VS 2013 SP4 and it Worked without issue...

Do you have SP4 applied? probably not this, but worth ruling out. As I didn't have any issues.

Code i tired without issue.

public class Program
{
    static void Main()
    {
        DoAsyncFoo();
        Console.ReadKey();
    }

    private static async void DoAsyncFoo()
    {
        var task = CollectStatsAsync();
        dynamic foo = await task;
        Console.WriteLine(foo.NumberOfCores);
    }

    private static async Task<dynamic> CollectStatsAsync()
    {
        return CollectStats();
    }

    private static dynamic CollectStats()
    {
        return new { NumberOfCores = 3 };
    }
}
Filamentous answered 21/9, 2015 at 20:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.