nat*_*dog 5 .net powershell assemblies
在.net项目的AssemblyInfo文件中,可以通过指定自定义程序集属性
[assembly:AssemblyMetadata("key1","value1")]
我的问题是如何通过PowerShell从已编译的.net程序集中检索此值?我能够读取所有标准属性,如fileversion,companyname等,但我有一点时间获取此自定义属性的值(key1)
尝试这样的事情:
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.
建立@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)
| 归档时间: |
|
| 查看次数: |
3027 次 |
| 最近记录: |