title: What is the type VoidTaskResult as it relates to async methods?
body: <p>I've been using async (and .Net 4.5 really) for the first time recently, and I've come across something that has me stumped. There isn't much information about the <code>VoidTaskResult</code> class that I can find on the Net so I came here to see if anyone has any ideas about what is going on.</p>
  
  <p>My code is something like the following. Obviously, this is much simplified. The basic idea is to call plugin methods, which are asynchronous. If they return <code>Task</code>, then there is no return value from the async call. If they return <code>Task&lt;&gt;</code>, then there is. We don't know in advance which type they are, so the idea is to look at the type of the result using reflection (<code>IsGenericType</code> is true if the type is <code>Type&lt;&gt;</code>) and get the value using a dynamic type.</p>
  
  <p>In my real code, I am calling the plugin method via reflection. I don't think this should make a difference to the behaviour I am seeing.</p>
  
  <pre><code>// plugin method
  public Task yada()
  {
   // stuff
  }
  
  public async void doYada()
  {
    Task task = yada();
    await task;
  
    if (task.GetType().IsGenericType)
    {
      dynamic dynTask = task;
      object result = dynTask.Result;
      // do something with result
    }
  }
  </code></pre>
  
  <p>This works good for the plugin method shown above. <code>IsGenericType</code> is false (as expected).</p>
  
  <p>However if you change the declaration of the plugin method ever so slightly, <code>IsGenericType</code> now returns true and stuff breaks:</p>
  
  <pre><code>public async Task yada()
  {
   // stuff
  }
  </code></pre>
  
  <p>When you do this, the following exception is thrown on the line <code>object result = dynTask.Result;</code>:</p>
  
  <p><img src="https://i.stack.imgur.com/6fvx9.png" alt="RuntimeBinderException"></p>
  
  <p>If you dig into the task object, it actually appears to be <code>Type&lt;VoidTaskResult&gt;</code>. <code>VoidTaskResult</code> is a private type in the Threading name space with almost nothing in it.</p>
  
  <p><img src="https://i.stack.imgur.com/YhlEm.png" alt="VoidTaskResult task"></p>
  
  <p>I tried changing my calling code:</p>
  
  <pre><code>public async void doYada()
  {
    Task task = yada();
    await task;
  
    if (task.GetType().IsGenericType)
    {
      object result = task.GetType().GetProperty("Result").GetMethod.Invoke(task, new object[] { });
      // do something with result
    }
  }
  </code></pre>
  
  <p>This "succeeds" in the sense that it no longer throws, but now result is of the type <code>VoidTaskResult</code> which I cannot sensibly do anything with.</p>
  
  <p>I should add that I'm having a hard time even formulating a real question for all this. Maybe my real question is something like "What is <code>VoidTaskResult</code>?", or "Why does this weird thing happen when I call an async method dynamically?" or possibly even "How do you call  plugin methods that are optionally asynchronous?" In any case, I am putting this out there in the hope that one of the gurus will be able to shed some light.</p>