How to get Custom Assembly Attribute with PowerShell

陌路散爱 提交于 2019-12-23 10:15:14

问题


In the AssemblyInfo files of a .net project one can specify a custom assembly attribute via

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

My question is how does one retrieve this value from a compiled .net assembly via powershell? I'm able to read all the standard attributes like fileversion, companyname, etc. but I'm having a heck of a time getting the value of this custom attribute (key1)


回答1:


Try something like this:

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

Copyright                                                   TypeId
---------                                                   ------
© Microsoft Corporation.  All rights reserved.              System.Reflection.AssemblyCopyrightAttribute

Instead of get-date use an instance from the assembly you're interested in. Also substitute the Assembly*Attribute that you are interested in retrieving.

In the specific case of AssemblyMetadataAttribute, it is new to .NET 4.5. PowerShell is still on .NET 4.0. So you have to use the reflection only context to get this attribute:

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

Spits out:

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()                  {}

Note the key1 ane value1 in the output.




回答2:


Building off @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) }

Gets you the AssemblyMetadata converted into a dictionary object. You can get just the value using:

$metadata["key1"]

Resulting in:

value1


来源:https://stackoverflow.com/questions/19168320/how-to-get-custom-assembly-attribute-with-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!