Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function LoadAssembly {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string[]]$asmNames,
[string]$baseDir = $PSScriptRoot
)
begin {
$domain = [System.AppDomain]::CurrentDomain
[Reflection.Assembly[]]$asms = $domain.GetAssemblies()
$cache = New-Object "System.Collections.Generic.HashSet[string]" (,[string[]]$asms.GetName().Name)
$path = $domain.getType().Assembly.Location
$searchPath = $baseDir,([IO.FileInfo]$path).Directory.FullName
}
process {
foreach ($asmName in $asmNames) {
LoadAssemblyInternal $asmName $searchPath $cache
}
}
}
function LoadAssemblyInternal([string[]]$asmNames, [string[]]$searchPath, $cache) {
foreach ($asmName in $asmNames) {
if (-not $cache.Contains($asmName)) {
$path = $searchPath | ls -filter "$asmName.dll" | select -first 1
if ($path -ne $null) {
Write-Verbose "Try Loading: $path"
[byte[]]$binary = [System.IO.File]::ReadAllBytes($path.FullName)
$asm = [System.Reflection.Assembly]::Load($binary);
$asm
Write-Verbose "Loaded: $($asm.FullName)"
$cache.Add($asm.GetName().Name) | Out-Null
LoadAssemblyInternal $asm.GetReferencedAssemblies().Name $searchPath $cache
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.