| File: GetAssemblyIdentity.cs | Web Access |
| Project: src\msbuild\src\Tasks\Microsoft.Build.Tasks.csproj (Microsoft.Build.Tasks.Core) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; using System.Collections.Generic; using System.Reflection; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; #if !NET using System.Globalization; using System.Text; #endif #nullable disable namespace Microsoft.Build.Tasks { /// <summary> /// Sniffs input files for their assembly identities, and outputs a set of items with the identity information. /// </summary> /// <comment> /// Input: Assembly Include="foo.exe" /// Output: Identity Include="Foo, Version=1.0.0.0", Name="Foo, Version="1.0.0.0" /// </comment> [MSBuildMultiThreadableTask] public class GetAssemblyIdentity : TaskExtension, IMultiThreadableTask { /// <inheritdoc /> public TaskEnvironment TaskEnvironment { get; set; } = TaskEnvironment.Fallback; private ITaskItem[] _assemblyFiles; [Required] public ITaskItem[] AssemblyFiles { get { ArgumentNullException.ThrowIfNull(_assemblyFiles, nameof(AssemblyFiles)); return _assemblyFiles; } set => _assemblyFiles = value; } [Output] public ITaskItem[] Assemblies { get; set; } private static string ByteArrayToHex(Byte[] a) { if (a == null) { return null; } #if NET return Convert.ToHexString(a); #else var s = new StringBuilder(a.Length * 2); foreach (Byte b in a) { s.Append(b.ToString("X02", CultureInfo.InvariantCulture)); } return s.ToString(); #endif } public override bool Execute() { var list = new List<ITaskItem>(); foreach (ITaskItem item in AssemblyFiles) { string assemblyPath = !string.IsNullOrEmpty(item.ItemSpec) ? TaskEnvironment.GetAbsolutePath(item.ItemSpec) : item.ItemSpec; AssemblyName an; try { an = AssemblyName.GetAssemblyName(assemblyPath); } catch (BadImageFormatException e) { Log.LogErrorWithCodeFromResources("GetAssemblyIdentity.CouldNotGetAssemblyName", item.ItemSpec, e.Message); continue; } catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e)) { Log.LogErrorWithCodeFromResources("GetAssemblyIdentity.CouldNotGetAssemblyName", item.ItemSpec, e.Message); continue; } ITaskItem newItem = new TaskItem(an.FullName); newItem.SetMetadata("Name", an.Name); if (an.Version != null) { newItem.SetMetadata("Version", an.Version.ToString()); } if (an.GetPublicKeyToken() != null) { newItem.SetMetadata("PublicKeyToken", ByteArrayToHex(an.GetPublicKeyToken())); } if (an.CultureInfo != null) { newItem.SetMetadata("Culture", an.CultureInfo.ToString()); } item.CopyMetadataTo(newItem); list.Add(newItem); } Assemblies = list.ToArray(); return !Log.HasLoggedErrors; } } }