如何获得自定义组件属性使用PowerShell [英] How to get Custom Assembly Attribute with PowerShell

查看:205
本文介绍了如何获得自定义组件属性使用PowerShell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个.NET项目的程序集信息文件,可以通过指定

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

[总成:AssemblyMetadata(键1,值1)]

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

我的问题是一个人如何检索通过PowerShell中编译.NET程序集这个值?我能念想文件版本,公司名称等,所有的标准属性,但我有一个时间赫克得到这个自定义属性的值(键1)

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)

推荐答案

尝试是这样的:

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.

在AssemblyMetadataAttribute的特定情况下,它是新的.NET 4.5。 PowerShell是仍然在.NET 4.0。所以,你必须使用反射唯一的上下文来获取这个属性:

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)

吐出来的:

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

请注意,在输出的键1 值1

这篇关于如何获得自定义组件属性使用PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆