Link to home
Start Free TrialLog in
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

How to get PublicKeyToken using C#

Using these two pieces of code how would I get the PublicKeyToken for a dll called Dynamics.Application.dll? How do I pass the name down as an argument?

using System;
using System.Reflection;

namespace EE_Q29126560
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine($"Object Assembly PublicKeyToken: {GetPublicKeyTokenFromAssembly(Assembly.GetAssembly(typeof(object)))}");
			Console.ReadLine();
		}

		static string GetPublicKeyTokenFromAssembly(Assembly assembly)
		{
			var bytes = assembly.GetName().GetPublicKeyToken();
			if (bytes == null || bytes.Length == 0)
				return "None";

			var result = string.Empty;
			for (int i = 0; i < bytes.GetLength(0); i++)
				result += $"{bytes[i]:x2}";
			return result;
		}
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwheeler23

ASKER

This worked perfectly.