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.

skynyrd
  • 942
  • 4
  • 14
  • 34
  • @AlexeiLevenkov it is not in another assembly. – skynyrd Sep 16 '15 at 15:06
  • @CodeCaster no, I already tried returning the expando object and I will attach it to the question. – skynyrd Sep 16 '15 at 15:06
  • 1
    @skynyrd ok. Now it is clear that [dynamic does not contain a definition for a property from a project reference](http://stackoverflow.com/questions/9416095/dynamic-does-not-contain-a-definition-for-a-property-from-a-project-reference) is not duplicate due to code being in the same assembly. – Alexei Levenkov Sep 16 '15 at 15:10
  • [Cannot reproduce](http://ideone.com/zLCXow). Please create a [mcve]. Do you perform this call over WCF or anything (which _does_ mean the `CollectStats` implementation is in another assembly)? – CodeCaster Sep 16 '15 at 15:19
  • 1
    @CodeCaster I have realized that it only happens in debug mode, then I've edited my question. Here is the sample code http://ideone.com/CLpHa9, If you put breakpoint to line 19, and evaluate foo.NumberOfCores in VS2013 QuickWatch screen, you will see that. – skynyrd Sep 17 '15 at 08:16
  • 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? :-) – CodeCaster Sep 17 '15 at 08:23
  • @CodeCaster I've just simplified it :) – skynyrd Sep 17 '15 at 08:58

1 Answers1

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 };
    }
}
Seabizkit
  • 2,417
  • 2
  • 15
  • 32