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)
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With