Skip to content

Commit

Permalink
(GH-36) Return PublicKeyToken as string
Browse files Browse the repository at this point in the history
  • Loading branch information
ferventcoder committed Jan 25, 2015
1 parent 749058e commit ce3a0a4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/chocolatey/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace chocolatey
{
using System.IO;
using System.Linq;
using infrastructure.adapters;

/// <summary>
Expand Down Expand Up @@ -59,5 +60,22 @@ public static Stream get_manifest_stream(this IAssembly assembly, string manifes
{
return assembly.GetManifestResourceStream(manifestResourceStreamLocation);
}

/// <summary>
/// Gets the public key token of an assembly.
/// </summary>
/// <param name="assembly">The assembly.</param>
/// <returns></returns>
/// <remarks>Borrowed heavily from http://dhvik.blogspot.com/2009/05/assemblynamegetpublickeytoken-tostring.html </remarks>
public static string get_public_key_token(this IAssembly assembly)
{
if (assembly == null) return string.Empty;

byte[] publicKeyToken = assembly.GetName().GetPublicKeyToken();

if (publicKeyToken == null || publicKeyToken.Length == 0) return string.Empty;

return publicKeyToken.Select(x => x.ToString("x2")).Aggregate((x, y) => x + y);
}
}
}
5 changes: 5 additions & 0 deletions src/chocolatey/infrastructure/adapters/Assembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace chocolatey.infrastructure.adapters
using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;

// ReSharper disable InconsistentNaming

Expand Down Expand Up @@ -66,6 +67,10 @@ public static IAssembly GetAssembly(Type type)
return new Assembly(System.Reflection.Assembly.GetAssembly(type));
//return System.Reflection.Assembly.GetAssembly(type);
}
public AssemblyName GetName()
{
return _assembly.GetName();
}

public static IAssembly GetExecutingAssembly()
{
Expand Down
3 changes: 3 additions & 0 deletions src/chocolatey/infrastructure/adapters/IAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace chocolatey.infrastructure.adapters
using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;

// ReSharper disable InconsistentNaming

Expand Down Expand Up @@ -64,6 +65,8 @@ public interface IAssembly
/// </returns>
string[] GetManifestResourceNames();

AssemblyName GetName();

/// <summary>
/// Loads the specified manifest resource from this assembly.
/// </summary>
Expand Down

0 comments on commit ce3a0a4

Please sign in to comment.