如何使用PowerShell获取自定义程序集属性

nat*_*dog 5 .net powershell assemblies

在.net项目的AssemblyInfo文件中,可以通过指定自定义程序集属性

[assembly:AssemblyMetadata("key1","value1")]

我的问题是如何通过PowerShell从已编译的.net程序集中检索此值?我能够读取所有标准属性,如fileversion,companyname等,但我有一点时间获取此自定义属性的值(key1)

Kei*_*ill 7

尝试这样的事情:

32# (get-date).GetType().Assembly.GetCustomAttributes([Reflection.AssemblyCopyrightAttribute], $false)

Copyright                                                   TypeId
---------                                                   ------
© Microsoft Corporation.  All rights reserved.              System.Reflection.AssemblyCopyrightAttribute
Run Code Online (Sandbox Code Playgroud)

而不是get-date使用您感兴趣的程序集中的实例.也可以替换您有兴趣检索的Assembly*Attribute.

在AssemblyMetadataAttribute的特定情况下,它是.NET 4.5的新增功能.PowerShell仍在.NET 4.0上.因此,您必须使用仅反射上下文来获取此属性:

$assembly = [Reflection.Assembly]::ReflectionOnlyLoadFrom("$pwd\ClassLibrary1.dll")
[reflection.customattributedata]::GetCustomAttributes($assembly)
Run Code Online (Sandbox Code Playgroud)

吐出:

AttributeType                 Constructor                   ConstructorArguments
-------------                 -----------                   --------------------
System.Runtime.Versioning.... Void .ctor(System.String)     {".NETFramework,Version=v4...
System.Reflection.Assembly... Void .ctor(System.String)     {"ClassLibrary1"}
System.Reflection.Assembly... Void .ctor(System.String)     {""}
System.Reflection.Assembly... Void .ctor(System.String)     {""}
System.Reflection.Assembly... Void .ctor(System.String)     {"CDL/TSO"}
System.Reflection.Assembly... Void .ctor(System.String)     {"ClassLibrary1"}
System.Reflection.Assembly... Void .ctor(System.String)     {"Copyright © CDL/TSO 2013"}
System.Reflection.Assembly... Void .ctor(System.String)     {""}
System.Reflection.Assembly... Void .ctor(System.String, ... {"key1", "value1"}
System.Runtime.InteropServ... Void .ctor(Boolean)           {(Boolean)False}
System.Runtime.InteropServ... Void .ctor(System.String)     {"945f04e1-dae3-4de6-adf6-...
System.Reflection.Assembly... Void .ctor(System.String)     {"1.0.0.0"}
System.Diagnostics.Debugga... Void .ctor(DebuggingModes)    {(System.Diagnostics.Debug...
System.Runtime.CompilerSer... Void .ctor(Int32)             {(Int32)8}
System.Runtime.CompilerSer... Void .ctor()                  {}
Run Code Online (Sandbox Code Playgroud)

注意输出中的key1ane value1.

  • `使用“1”个参数调用“GetCustomAttributes”时出现异常:“无法解析对程序集“WebActivatorEx,Version=2.0.0.0,Culture=neutral,PublicKeyToken=7b26dc2a43f6a0d4”的依赖关系,因为它尚未预加载。使用 ReflectionOnly API 时,依赖程序集必须通过 ReflectionOnlyAssemblyResolve 事件预先加载或按需加载。"` 看起来此方法不允许具有依赖项的程序集。 (2认同)

Aar*_*n D 7

建立@Keith Hill -

$assembly = [Reflection.Assembly]::ReflectionOnlyLoadFrom("$pwd\ClassLibrary1.dll")
$metadata = @{}
[reflection.customattributedata]::GetCustomAttributes($assembly) | Where-Object {$_.AttributeType -like "System.Reflection.AssemblyMetadataAttribute"} | ForEach-Object { $metadata.Add($_.ConstructorArguments[0].Value, $_.ConstructorArguments[1].Value) }
Run Code Online (Sandbox Code Playgroud)

获取转换为字典对象的AssemblyMetadata.你可以使用以下方法获得价值:

$metadata["key1"]
Run Code Online (Sandbox Code Playgroud)

导致:

value1
Run Code Online (Sandbox Code Playgroud)

  • 对我来说失败了。`使用“1”个参数调用“GetCustomAttributes”时出现异常:“无法解析对程序集“WebActivatorEx,Version=2.0.0.0,Culture=neutral,PublicKeyToken=7b26dc2a43f6a0d4”的依赖关系,因为它尚未预加载。使用 ReflectionOnly API 时,依赖程序集必须通过 ReflectionOnlyAssemblyResolve 事件预先加载或按需加载。"` 看起来此方法不允许具有依赖项的程序集。 (2认同)