We always create separate assemblies so that we can reuse some of the classes, and declare the classes as public so that can be accessed by other assemblies. However this will allow other people to use the assemblies that we created. To protect our assemblies to be assessed by our owned assemblies only, we can declare the classes as internal and do the following setup:
For the assembly that will access your shared assembly
- Use SN utility to create a new key pair and save it in an out put file (key.snk). SN is available in .Net SDK.
sn -k key.snk
- Use your .Net IDE (either Visual Studio or Sharpdevelop) to sign the assembly with the key file (key.snk).
- Compile the assembly.
- Use SECUTIL to extract the strong name information from the assembly (usually in the bin\debug directory in you project folder) in hex string format and save it to a text file (strong.txt). SECUTIL is also available in .Net SDK.
secutil -hex -s application.exe > strong.txt
- Inside the strong name file (strong.txt), you can see a long hex string, copy it (remember to exclude the first 2 character 0x)
For the shared assembly
- Use SN utility to create a another key pair and save it in an out put file (key1.snk).
sn -k key1.snk
- Use your .Net IDE (either Visual Studio or Sharpdevelop) to sign the assembly with the key file (key1.snk).
- Open AssemblyInfo.cs, (or AssemblyInfo.vb, depend on the language that you used)
- Be sure System.Runtime.Compiler.Services is imported:
using System.Runtime.CompilerServices;
- Go to last line of the file, add the following line (ProgramName is the name of you referring assembly and hexstring is the hex string that you copied from the strong name file just now)
[assembly: InternalsVisibleTo("ProgramName, PublicKey=hexstring")]
- Compile the assembly.
Now you should able to refer the shared assembly and use the internal classes.
You might not need to install SDK to get SN or SECUTIL, try google around should able to find some download links.