Skip to content

Instantly share code, notes, and snippets.

@ritalin
Created February 10, 2016 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritalin/7804d03d1d9b04f6e8df to your computer and use it in GitHub Desktop.
Save ritalin/7804d03d1d9b04f6e8df to your computer and use it in GitHub Desktop.
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