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.
CollectStats
implementation is in another assembly)? – StavangerConsole.WriteLine()
does print3
... Weird behavior. Perhaps reduce your question to show the latest code that reproduces it, for brevity? :-) – Stavanger