Skip to content

Instantly share code, notes, and snippets.

@tfwio
Last active August 29, 2015 14:02
Show Gist options
  • Save tfwio/b357a0b3a75e59d43aed to your computer and use it in GitHub Desktop.
Save tfwio/b357a0b3a75e59d43aed to your computer and use it in GitHub Desktop.
Get StrongKeyName and StrongKey from an assembly (windows batch command)

How to implement InternalsVisibleTo with PublicKey

  1. Verify the location of your DOTNET SDK directory. You simply need sn.exe available to the batch script. Modify the sdkdir variable to a directory containing the tool.
  2. DragDrop the assembly you need a key from onto the batch-script provided.
  3. Note that 'Your-AssemblyName' (as in the demo assemblyinfo.cs) is stored within the csproj main PropertyGroup and of course, is called AssemblyName.
  4. Once you performed step-1, you need to clean out the whitespace between line-segments of the PublicKey so that it reads on one line---careful to not make any errors, otherwise you will get compile errors and the dependant assembly will not see the internals.
  5. Be careful copy/pasting the key into the appropriate location, within your AssemblyInfo.cs file.
  6. Compile

TIP: Note that the first few times you use the InternalsVisibleAttribute can be a pain until you get used to it. You might want to use the same key-file in both assemblies to get started. Dont copy or duplicate the keyfile, use the 'browse' feature of VisualStudio's project-settings to use the exact same keyfile.

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// to be added to your AssemblyInfo.cs
// do not overwrite your assemblyinfo.cs file with this one!
[assembly: InternalsVisibleTo("[Your-AssemblyName],PublicKey=...")]
::@echo off
:: =========================================================
:: change this corresponding to the location of sn.exe
:: if this script does not work.
:: =========================================================
:: POSSIBLE TOOL LOCATIONS ON WIN7x64
:: =========================================================
:: C:\Program Files\Microsoft.NET\SDK\v2.0 64bit\bin
:: C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin
:: C:\Program Files\Microsoft SDKs\Windows\v7.0\bin
:: C:\Program Files\Microsoft SDKs\Windows\v7.1\bin
:: C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools
:: C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools
:: =========================================================
set sdkdir=C:\Program Files\Microsoft SDKs\Windows\v7.1\bin
:: =========================================================
:: generate publickeytoken
:: "%sdkdir%/sn.exe" -T "%~1"
:: =========================================================
:: generate publickey and publickeytoken
"%sdkdir%/sn.exe" -Tp "%~1">"%~dpn1.token.txt"
:: PRODUCE COMMA DELIMITED INTEGER VALUES
:: =========================================================
:: generate publickey and publickeytoken
:: "%sdkdir%\sn.exe" -o "%~1" "%~dpn1.token.txt"
:: =========================================================
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment