2013-10-03 9 views
4

와 속성 얻는 방법 [조립 : AssemblyMetadata ("키 1", "값 1")]사용자 정의 조립 한</p> <p>를 통해 사용자 지정 어셈블리 속성을 지정할 수 있습니다 .NET 프로젝트의 어셈블리 정보 파일에서 PowerShell을

내 질문은 어떻게 컴파일 된 .net 어셈블리에서이 값을 powershell 통해 검색합니까? 나는 등 FILEVERSION, COMPANYNAME, 같은 모든 표준 속성을 읽을 수 있어요하지만 난이 사용자 정의 속성의 값을 얻는 시간의 지옥을 보내고있어 (키 1)

답변

6

이 같은 시도 :

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

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

get-date 대신에 관심이있는 어셈블리의 인스턴스를 사용하십시오. 또한 검색에 관심이있는 Assembly * Attribute를 사용하십시오.

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 그래서 당신은이 속성을 얻을 수있는 반사에게 유일한 컨텍스트를 사용합니다. @Keith 힐을 구축

+0

감사하지만 난 뭔가를 많이 이미 형제의 시도했습니다. 작동하는 스 니펫 (snippet)은 어떻습니까? –

+0

@natedog 업데이트 된 답변보기 이 스 니펫이 작동합니까? –

3

-

$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