Permalink
Browse files

Use Reflection.Metadata package to read assembly info

The previous approach when we tried to load assembly doesn't work
well for the different target frameworks as dependencies might be
missing.
  • Loading branch information...
zvirja committed Oct 27, 2017
1 parent c019712 commit 57c212d0e4b966b1537fb90af535aec56dfeb0bd
@@ -36,77 +36,5 @@ private object ExecuteCode(string assemblyPath, string typeName, string methodNa
return null;
}
public AssemblyMetaData GetAssemblyMetadata(string assemblyPath)
{
var data = new AssemblyMetaData();
var assemblyName = AssemblyName.GetAssemblyName(assemblyPath);
if (assemblyName == null)
{
return data;
}
// For WinRT component, we can only read Full Name.
if (assemblyName.ContentType == AssemblyContentType.WindowsRuntime)
{
data.SetFullName(assemblyName.FullName);
return data;
}
var assembly = Assembly.Load(assemblyName);
if (assembly != null)
{
data.SetFullName(assembly.FullName);
try
{
foreach (var attribute in assembly.GetCustomAttributesData())
{
if (attribute.ConstructorArguments.Count != 1 ||
attribute.ConstructorArguments[0].ArgumentType != typeof(string))
{
continue;
}
string typeName = attribute.Constructor.DeclaringType.Name;
if (typeName == "InternalsVisibleToAttribute")
{
continue;
}
string key = typeName.EndsWith("Attribute", StringComparison.OrdinalIgnoreCase)
? typeName.Substring(0, typeName.Length - 9)
: typeName;
string value = attribute.ConstructorArguments[0].Value.ToString();
if (!String.IsNullOrEmpty(value))
{
data[key] = value;
}
}
}
catch (Exception)
{
// if an exception occurs when loading custom attributes, just ignore
}
try
{
data[AssemblyMetaData.ReferencedAssembliesKey] = string.Join(
Environment.NewLine,
assembly.GetReferencedAssemblies().OrderBy(assName => assName.Name)
);
}
catch
{
// Ignore if unable to obtain referenced assemblies
}
}
return data;
}
}
}

This file was deleted.

Oops, something went wrong.
@@ -26,18 +26,6 @@ public static bool IsNpeMetroInstalled
}
}
public static AssemblyMetaData GetAssemblyMetadata(string assemblyPath)
{
if (!File.Exists(assemblyPath))
{
return null;
}
AssemblyMetaData result = null;
ExecuteRemotely(worker => result = worker.GetAssemblyMetadata(assemblyPath));
return result;
}
private static void ExecuteRemotely(Action<AppDomainWorker> workerAction)
{
AppDomain domain = AppDomain.CreateDomain("CodeInvoker");
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace NuGetPe.AssemblyMetadata
{
/// <summary>
/// Meta data of the assembly,
/// </summary>
public class AssemblyMetaData : Dictionary<string, string>
{
public const string ReferencedAssembliesKey = "Referenced assemblies";
/// <summary>
/// Set Fullname of the assembly and determine strong name.
/// </summary>
/// <remarks>Helper</remarks>
public void SetFullName(AssemblyName assemblyName)
{
this[FullNameLabel] = assemblyName.FullName;
try
{
var publicKey = assemblyName.GetPublicKeyToken();
var isStrongNamed = publicKey != null && publicKey.Length > 0;
if (isStrongNamed)
{
this[StrongNamedLabel] = $"Yes, version {assemblyName.Version}";
}
else
{
this[StrongNamedLabel] = "No";
}
}
catch (Exception)
{
//ignore
}
}
private const string FullNameLabel = "Full Name";
private const string StrongNamedLabel = "Strong Name";
}
}
Oops, something went wrong.

0 comments on commit 57c212d

Please sign in to comment.