【发布时间】:2020-01-22 15:28:01
【问题描述】:
ps1 脚本应该循环遍历 /bin 文件夹中的程序集,提取属性,然后构造一个带有匹配项的 json 文件。这对 99% 的 DLLS 都有效。
但一个 DLL 是在构建时引用了一个旧程序集
Exception calling "GetCustomAttributes" with "1" argument(s): "Could not load file or assembly
'Telerik.Sitefinity.Frontend, Version=12.2.7200.0, Culture=neutral, PublicKeyToken=b28c218413bdf563' or one of its
dependencies. The system cannot find the file specified."
文件夹中的版本是12.2.7225.0
所以这个程序集基本跳过了……
当 webapp 运行时这不是问题,因为 web.config 程序集绑定只是很好地处理版本不匹配,但在 powershell 中,它正在失败。
param(
[Parameter(Mandatory=$true)]
[string]$binariesDirectory
)
$assemblies = Get-ChildItem $binariesDirectory -Filter *.dll
$controllerAssemblies = @()
foreach ($assembly in $assemblies) {
$loadedAssembly = [System.Reflection.Assembly]::LoadFrom($assembly.FullName)
#THIS IS THE TEST
if($assembly.Name -eq "RandomSiteControlsMVC.dll"){
Write-Output "Custom Attributes for " + $assembly.Name;
#THIS IS WHAT FAILS
$result = [reflection.customattributedata]::GetCustomAttributes($loadedAssembly)
Write-Output $result;
}
#$loadedAssembly.CustomAttributes just returns nothing in the case of that DLL
if ($loadedAssembly.CustomAttributes | ? { $_.AttributeType.FullName -eq "Telerik.Sitefinity.Frontend.Mvc.Infrastructure.Controllers.Attributes.ControllerContainerAttribute" -or $_.AttributeType.FullName -eq "Telerik.Sitefinity.Frontend.Mvc.Infrastructure.Controllers.Attributes.ResourcePackageAttribute"}) {
$controllerAssemblies += $assembly.Name
}
}
$controllerAssemblies | ConvertTo-Json -depth 100 | Set-Content "$binariesDirectory\ControllerContainerAsembliesLocation.json" -Force
有没有什么神奇的方法可以在不抛出异常的情况下正常工作?
【问题讨论】:
标签: powershell assemblies