Skip to content

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

Description

@SeeminglyScience

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()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions