Skip to content

Instantly share code, notes, and snippets.

@heaths
Created September 28, 2022 18:30
Show Gist options
  • Save heaths/c15a0818bed06b68fb171252db3b1c64 to your computer and use it in GitHub Desktop.
Save heaths/c15a0818bed06b68fb171252db3b1c64 to your computer and use it in GitHub Desktop.
Gets exported types from one or more assemblies.
[CmdletBinding(DefaultParameterSetName = 'Path')]
param (
[Parameter(ParameterSetName = 'Path', Position = 0, Mandatory = $true)]
[string[]] $Path,
[Parameter(ParameterSetName = 'LiteralPath', Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[Alias('PSPath')]
[string[]] $LiteralPath
)
if ($PSCmdlet.ParameterSetName -eq 'Path') {
$LiteralPath = $Path | Resolve-Path
}
foreach ($p in $LiteralPath) {
$file = [System.IO.File]::OpenRead($p)
try {
$pe = [System.Reflection.PortableExecutable.PEReader]::new($file)
try {
$meta = [System.Reflection.Metadata.PEReaderExtensions]::GetMetadataReader($pe)
foreach ($typeHandle in $meta.TypeDefinitions) {
$type = $meta.GetTypeDefinition($typeHandle)
$attr = $type.Attributes
if ($attr -band 'Public' -and !$type.IsNested) {
[pscustomobject]@{
Name = $meta.GetString($type.Name)
Namespace = $meta.GetString($type.Namespace)
}
}
}
} finally {
$pe.Dispose()
}
} finally {
$file.Dispose()
}
}
<#
.SYNOPSIS
Gets exported types from one or more assemblies.
.DESCRIPTION
Gets all the public, non-nested types in given assemblies. No assembly resolution is necessary, so dependencies do not need to be loaded.
.PARAMETER Path
One or more assembly file paths. Wildcards are supported.
.PARAMETER LiteralPath
One or more resolved assembly file paths. Wildcards are not supported.
.EXAMPLE
Get-ExportedTypes.ps1 Assembly.dll | Select-Object Namespace -Unique
Gets all namespaces defined by types in the assembly.
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment