How to get custom assembly attribute using PowerShell

In the .net project's AssemblyInfo files, you can specify your own assembly attribute through

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

My question is how to get this value from a compiled .net assembly via powershell? I can read all the standard attributes such as fileversion, companyname, etc., but I have time to get the value of this custom attribute (key1)

+4
source share
2 answers

Try something like this:

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

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

Instead, get-dateuse the instance from the assembly you are interested in. Also replace the Assembly * attribute you want to get.

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 ane value1 .

+6

@ -

$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
+3

All Articles