PSLambda icon indicating copy to clipboard operation
PSLambda copied to clipboard

Linq extension method resolution fails with some custom IEnumerable<> types

Open SeeminglyScience opened this issue 7 years ago • 0 comments

I need better detection interfaces in method resolution. As a workaround you can cast it as an IEnumerable<>.

using namespace System.Collections.Generic
using namespace System.IO
using namespace System.Reflection.PortableExecutable
using namespace System.Reflection.Metadata

$getNamespaceDelegate = [psdelegate]{
    $fileStream = [FileStream]::new(
        [psobject].Assembly.Location,
        [FileMode]::Open,
        [FileAccess]::Read,
        [FileShare]::ReadWrite)

    with ($fileStream) {
        with ($peReader = [PEReader]::new($fileStream)) {
            $mdReader = $peReader.GetMetadataReader()

            # Here's the example
            $foundHandle = ([IEnumerable[TypeDefinitionHandle]]$mdReader.TypeDefinitions).First{
                $handle => ($mdReader.GetString($mdReader.GetTypeDefinition($handle).Name) -eq 'PowerShell')
            }

            return $mdReader.GetString($mdReader.GetTypeDefinition($foundHandle).Namespace)
        }
    }
}

$getNamespaceDelegate.Invoke()

SeeminglyScience avatar Jun 01 '18 13:06 SeeminglyScience