在PowerShell脚本中获取程序集文件属性的指定元数据特性的方法——AssemblyMetadataAttribute
在PowerShell脚本中获取程序集文件属性的指定元数据特性的方法——AssemblyMetadataAttribute
<#
.SYNOPSIS
获取程序集文件属性的指定元数据特性
.DESCRIPTION
获取程序集文件属性的指定元数据特性
.PARAMETER filePath
程序集文件路径
.PARAMETER name
元数据名称
.EXAMPLE
PS C:\> Get-AssemblyMetadata -filePath 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.4\System.Text.Json.dll' -name 'RepositoryUrl'
https://github.com/dotnet/runtime
.NOTES
Additional information about the function.
#>
function Get-AssemblyMetadata
{
[OutputType([string])]
param
(
[Parameter(Mandatory = $true, HelpMessage = '程序集文件路径')]
[Alias('p')]
[string]$filePath,
[Parameter(Mandatory = $true, HelpMessage = '元数据名称')]
[Alias('n')]
[string]$name
)
if (-not (Test-Path $filePath))
{
return ""
}
## $asm = [System.Reflection.Assembly]::LoadFile($filePath)
## $data = $asm.CustomAttributes
$buffer = [System.IO.File]::ReadAllBytes($filePath)
$asm = [System.Reflection.Assembly]::Load($buffer)
$data = [System.Reflection.CustomAttributeData]::GetCustomAttributes($asm)
foreach ($attr in $data)
{
if ($attr.AttributeType.Name -eq 'AssemblyMetadataAttribute' -and $attr.ConstructorArguments.Count -eq 2)
{
$nameArg = $attr.ConstructorArguments[0];
$valueArg = $attr.ConstructorArguments[1];
if ($nameArg.Value -eq $name)
{
return $valueArg.Value
}
}
}
return ""
}
使用示例
PS C:\> Get-AssemblyMetadata -filePath 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.4\System.Text.Json.dll' -name 'RepositoryUrl'
https://github.com/dotnet/runtime
作者:VAllen
出处:http://www.cnblogs.com/vallen
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
唯有偏执者得以生存。
出处:http://www.cnblogs.com/vallen
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
唯有偏执者得以生存。
分类:
PowerShell
标签:
powershell
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10亿数据,如何做迁移?
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 易语言 —— 开山篇
· Trae初体验
2014-04-26 Microsoft Visual Studio Ultimate 2013 Update 2 RC 英文版--离线完整安装ISO+简体中文语言包