Udostępnij za pośrednictwem


How to: Reference 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

A strong-named assembly can only use types from other strong-named assemblies. Otherwise, the security of the strong-named assembly would be compromised.

To make a compile-time reference to a strong-named assembly

  • At the command prompt, type the following command:

    <compiler command> /reference:<assembly name>

    where 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 myAssembly.cs.

csc /t:library myAssembly.cs /reference:myLibAssembly.dll

To make a run-time reference to a strong-named assembly

  • When you make a run-time reference to a strong-named assembly (for example, by using the Assembly.Load or Assembly.GetType method), 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>, <public key token>

    For example:

    myDll, Version=1.1.0.0, 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=1.0.0.1,Culture=neutral,PublicKeyToken=9b35aa32c18d4fb1")
Assembly.Load("myDll,Version=1.0.0.1,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):

sn -tp <assembly>

See Also

Other Resources

Creating and Using Strong-Named Assemblies