【发布时间】:2013-10-03 20:08:22
【问题描述】:
在 .net 项目的 AssemblyInfo 文件中,可以通过
指定自定义程序集属性[组装:组装元数据(“key1”,“value1”)]
我的问题是如何通过 powershell 从已编译的 .net 程序集中检索此值?我能够读取所有标准属性,如文件版本、公司名称等,但我很难获得这个自定义属性 (key1) 的值
【问题讨论】:
标签: .net powershell assemblies
在 .net 项目的 AssemblyInfo 文件中,可以通过
指定自定义程序集属性[组装:组装元数据(“key1”,“value1”)]
我的问题是如何通过 powershell 从已编译的 .net 程序集中检索此值?我能够读取所有标准属性,如文件版本、公司名称等,但我很难获得这个自定义属性 (key1) 的值
【问题讨论】:
标签: .net powershell assemblies
试试这样的:
32# (get-date).GetType().Assembly.GetCustomAttributes([Reflection.AssemblyCopyrightAttribute], $false)
Copyright TypeId
--------- ------
© Microsoft Corporation. All rights reserved. System.Reflection.AssemblyCopyrightAttribute
使用您感兴趣的程序集中的实例代替 get-date。还替换您有兴趣检索的 Assembly*Attribute。
在 AssemblyMetadataAttribute 的特定情况下,它是 .NET 4.5 的新内容。 PowerShell 仍在 .NET 4.0 上。所以你必须使用仅反射上下文来获取这个属性:
$assembly = [Reflection.Assembly]::ReflectionOnlyLoadFrom("$pwd\ClassLibrary1.dll")
[reflection.customattributedata]::GetCustomAttributes($assembly)
吐出:
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() {}
注意输出中的key1 和value1。
【讨论】:
Exception calling "GetCustomAttributes" with "1" argument(s): "Cannot resolve dependency to assembly 'WebActivatorEx, Version=2.0.0.0, Culture=neutral, PublicKeyToken=7b26dc2a43f6a0d4' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event." 看起来此方法不允许具有依赖项的程序集。
以@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) }
让您将 AssemblyMetadata 转换为字典对象。您可以使用以下方法获取值:
$metadata["key1"]
导致:
value1
【讨论】:
Exception calling "GetCustomAttributes" with "1" argument(s): "Cannot resolve dependency to assembly 'WebActivatorEx, Version=2.0.0.0, Culture=neutral, PublicKeyToken=7b26dc2a43f6a0d4' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event." 看起来这个方法不允许有依赖的程序集。