Setup: on Windows 10 / PS 5.1, run this to get access to the WindowsRuntime and the WinRT types I'm using:
Add-Type -AssemblyName System.Runtime.WindowsRuntime
[Windows.Foundation.IAsyncAction,Windows.Foundation,ContentType=WindowsRuntime]
[Windows.Foundation.IAsyncOperation`1,Windows.Foundation,ContentType=WindowsRuntime]
[Windows.Foundation.IAsyncOperationWithProgress`2,Windows.Foundation,ContentType=WindowsRuntime]
OK, now find an extension method I'm not looking for - AsTask() which takes one parameter, typed as an [IAsyncAction]:
[System.WindowsRuntimeSystemExtensions].GetMethod('AsTask',
[Windows.Foundation.IAsyncAction])
There will be some output - a method found.
Now try for the one I am looking for, the same AsTask() method, but this time the overload which takes a parameter typed IAsyncOperation<T> or [IAsyncOperation`1]:
[System.WindowsRuntimeSystemExtensions].GetMethod('AsTask',
[Windows.Foundation.IAsyncOperation`1])
No output. No output if the type name is given as a string instead.
But that overload does exist; ask for all the methods and filter them afterwards, and this will find it:
([System.WindowsRuntimeSystemExtensions].GetMethods() |
Where-Object { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and
$_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
This last block of code is what I am using and it works, the question which led me here was: can I ask for that method directly from GetMethod() in one call?
The types from the generic "AsTask"-method and [Windows.Foundation.IAsyncOperation`1] are not the same even if they have the same type-GUID. They are different in 4 parameters, but they are read-only:
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$methods = [System.WindowsRuntimeSystemExtensions].GetMethods()
$taskList = $methods | ?{$_.Name -eq "AsTask"}
$asTask = $taskList | ?{$_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' }
$type1 = $asTask.GetParameters().ParameterType
$type2 = [Windows.Foundation.IAsyncOperation`1]
$attribList = ("IsGenericTypeDefinition", "IsConstructedGenericType", "GenericTypeParameters", "GenericTypeArguments")
foreach ($attrib in $attribList) {
"$attrib : $($type1.$attrib) -> $($type2.$attrib)"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With