Remove allocations from Dns.* (dotnet/corefx#41061)
This started as an effort to reduce the size of System.Net.NameResolution.dll when publishing a trimmed app. It's not that big to begin with, but it's carrying around a copy of all of the IAsyncResult helper types, because the Get*Async methods are currently wrappers for the Begin/End* methods.
This PR inverts that, wrapping the Begin/End* methods instead around the Get*Async methods, using the same TaskToApm helper we use in other places in corefx for the same purpose. This makes the Get*Async methods faster and lighterweight, but it does increase the number/amount of allocation in the Begin/End* APIs. Since these are considered legacy, I normally would consider that a good trade, however we still use these Begin/End methods in a few places in System.Net.Sockets, and I didn't want to regress those use cases.
So, this also then trims some additional fat, which helps the Get*Async cases even further, and gets the Begin/End* to be even better than before the change. This includes not allocating an IPHostEntry when we're just going to unwrap it and return its addresses, computing the exact IPAddress[] size we need rather than using a List<> to grow it and ToArray to create the actual array, avoiding creating the HostName if we don't need it, and avoiding an unnecessary SafeHandle allocation.
As part of this, I also noticed that we had some bugs in how some of our interop structures on Windows were defined. In particular, fields that in the native types were size_t were defined as int rather than IntPtr in the managed code. It appears we were saved by padding, but I fixed it regardless.
And as long as I was changing pretty much everything else, where I was touching code I also cleaned up some legacy style stuff.
Commit migrated from https://github.com/dotnet/corefx/commit/
a55e95cbd9fca7ce5b3ad34c583642a35f42cd63