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
有没有一种神奇的方法可以在不抛出异常的情况下正常工作?
使用Try Catch语句捕获错误。然后,在catch语句中,由于dll无法记录日志或...根本什么也没有,您可以执行操作。
try {
$loadedAssembly = [System.Reflection.Assembly]::LoadFrom('blurb.dll')
}
catch {
# Do something (log it... or not)
}