This example shows how to use friend assemblies with assemblies that have strong names. Both assemblies must be strong named. Although both assemblies in this example use the same keys, you could use different keys for two assemblies.
Create a signed assembly and a friend assembly
Open a command prompt.
Use the following sequence of commands with the Strong Name tool to generate a keyfile and to display its public key. For more information, see Sn.exe (Strong Name tool).
Generate a strong-name key for this example and store it in the file FriendAssemblies.snk:
sn -k FriendAssemblies.snk
Extract the public key from FriendAssemblies.snk and put it into FriendAssemblies.publickey:
sn -p FriendAssemblies.snk FriendAssemblies.publickey
Display the public key stored in the file FriendAssemblies.publickey:
sn -tp FriendAssemblies.publickey
Create a C# or Visual Basic file named friend_signed_A that contains the following code. The code uses the InternalsVisibleToAttribute attribute to declare friend_signed_B as a friend assembly.
The Strong Name tool generates a new public key every time it runs. Therefore, you must replace the public key in the following code with the public key you just generated, as shown in the following example.
' friend_signed_A.vb
' Compile with:
' Vbc -target:library -keyfile:FriendAssemblies.snk friend_signed_A.vb
Imports System.Runtime.CompilerServices
<Assembly: InternalsVisibleTo("friend_signed_B, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e3aedce99b7e10823920206f8e46cd5558b4ec7345bd1a5b201ffe71660625dcb8f9a08687d881c8f65a0dcf042f81475d2e88f3e3e273c8311ee40f952db306c02fbfc5d8bc6ee1e924e6ec8fe8c01932e0648a0d3e5695134af3bb7fab370d3012d083fa6b83179dd3d031053f72fc1f7da8459140b0af5afc4d2804deccb6")>
Public Class Class1
Public Sub Test()
System.Console.WriteLine("Class1.Test")
System.Console.ReadLine()
End Sub
End Class
Compile and sign friend_signed_A by using the following command.
Create a C# or Visual Basic file named friend_signed_B that contains the following code. Because friend_signed_A specifies friend_signed_B as a friend assembly, the code in friend_signed_B can access internal (C#) or Friend (Visual Basic) types and members from friend_signed_A. The file contains the following code.
' friend_signed_B.vb
' Compile with:
' Vbc -keyfile:FriendAssemblies.snk -r:friend_signed_A.dll friend_signed_B.vb
Module Sample
Public Sub Main()
Dim inst As New Class1
inst.Test()
End Sub
End Module
Compile and sign friend_signed_B by using the following command.
The name of the assembly generated by the compiler must match the friend assembly name passed to the InternalsVisibleToAttribute attribute. You must explicitly specify the name of the output assembly (.exe or .dll) by using the -out compiler option. For more information, see OutputAssembly (C# compiler options) or -out (Visual Basic).
Джерело цього вмісту можна знайти на GitHub, де також можна створювати й переглядати запитання та запити на внесення змін. Докладні відомості наведено в нашому посібнику для співавторів.
Відгук про .NET
.NET – це проект із відкритим кодом. Виберіть посилання, щоб надати відгук:
Дізнайтеся, як впроваджувати класи за допомогою розширених методів, таких як статичні класи, часткові класи та ініціалізатори об'єктів, які можуть підвищити зручність читання, підтримання та організацію коду.