Referencing a Strong-Named Assembly
The process for referencing types or resources in a strong-named assembly is usually transparent. You can make the reference either at compile time (early binding) or at run time.
A compile-time reference occurs when you indicate to the compiler that your assembly explicitly references another assembly. When you use compile-time referencing, the compiler automatically gets the public key of the targeted strong-named assembly and places it in the assembly reference of the assembly being compiled.
Note All assemblies that reference types in a strong-named assembly must also have a strong name.
To make a compile-time reference to a strong-named assembly
- At the command prompt, type the following command:
<compiler command> /reference:<assembly name>
In this command, compiler command is the compiler command for the language you are using, and assembly name is the name of the strong-named assembly being referenced. You can also use other compiler options, such as the /t:library option for creating a library assembly.
The following example creates an assembly called
myAssembly.dll that references a strong-named assembly called
myLibAssembly.dll from a code module called
csc /t:library myAssembly.cs /reference:myLibAssembly.dll
To make an assembly reference at run time, such as when using the Assembly.Load or Assembly.GetType methods, you must use the display name of the referenced strong-named assembly. The syntax of a display name is as follows:
<assembly name>, <version number>, <culture>, <strong name>
myDll, Version=18.104.22.168, Culture=en, PublicKeyToken=03689116d3a4ae33
In this example, the PublicKeyToken is the hexadecimal form of the public key token. If there is no culture value, use Culture=neutral.
The following code example shows how to use this information with the Assembly.Load method.
Assembly.Load("myDll,Version=22.214.171.124,Culture=neutral,PublicKeyToken=9b35aa32c18d4fb1") [C#] Assembly.Load("myDll,Version=126.96.36.199,Culture=neutral,PublicKeyToken=9b35aa32c18d4fb1");
You can print the hexadecimal format of the public key and public key token for a specific assembly using the following Strong Name tool (Sn.exe) command:
sn -Tp <assembly>
If you have a public key file, you can use the following command instead (note the difference in case on the command-line option):