InternalsVisibleToAttribute(String) Konstruktor
Definicja
Ważny
Niektóre informacje dotyczą produktów przedpremierowych, które mogą zostać znacznie zmodyfikowane przed premierą. Microsoft nie udziela żadnych gwarancji, ani wyraźnych, ani domniemanych, dotyczących informacji podanych tutaj.
Inicjuje nowe wystąpienie klasy InternalsVisibleToAttribute o nazwie określonego zestawu znajomego.
public:
InternalsVisibleToAttribute(System::String ^ assemblyName);
public InternalsVisibleToAttribute(string assemblyName);
new System.Runtime.CompilerServices.InternalsVisibleToAttribute : string -> System.Runtime.CompilerServices.InternalsVisibleToAttribute
Public Sub New (assemblyName As String)
Parametry
- assemblyName
- String
Nazwa znajomego zestawu.
Przykłady
podpisanych zestawów
W poniższym przykładzie użyto atrybutu InternalsVisibleToAttribute, aby utworzyć metodę internal o nazwie AppendDirectorySeparator w podpisanym zestawie widocznym dla innego podpisanego zestawu. Definiuje klasę FileUtilities zawierającą wewnętrzną metodę AppendDirectorySeparator. Atrybut InternalsVisibleToAttribute jest stosowany do zestawu zawierającego klasę FileUtilities. Atrybut umożliwia zestawowi o nazwie Friend1 uzyskiwanie dostępu do tego wewnętrznego elementu członkowskiego.
//
// The source code should be saved in a file named Example1.cs. It
// can be compiled at the command line as follows:
//
// csc /t:library /keyfile:<snkfilename> Assembly1.cs
//
// The public key of the Friend1 file should be changed to the full
// public key stored in your strong-named key file.
//
using System;
using System.IO;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Friend1, PublicKey=002400000480000094" +
"0000000602000000240000525341310004000" +
"001000100bf8c25fcd44838d87e245ab35bf7" +
"3ba2615707feea295709559b3de903fb95a93" +
"3d2729967c3184a97d7b84c7547cd87e435b5" +
"6bdf8621bcb62b59c00c88bd83aa62c4fcdd4" +
"712da72eec2533dc00f8529c3a0bbb4103282" +
"f0d894d5f34e9f0103c473dce9f4b457a5dee" +
"fd8f920d8681ed6dfcb0a81e96bd9b176525a" +
"26e0b3")]
public class FileUtilities
{
internal static string AppendDirectorySeparator(string dir)
{
if (!dir.Trim().EndsWith(Path.DirectorySeparatorChar.ToString()))
return dir.Trim() + Path.DirectorySeparatorChar;
else
return dir;
}
}
'
' The source code should be saved in a file named Example1.cs. It
' can be compiled at the command line as follows:
'
' vbc Assembly1.vb /t:library /keyfile:<snkfilename>
'
' The public key of the Friend1 file should be changed to the full
' public key stored in your strong-named key file.
'
Imports System.IO
Imports System.Runtime.CompilerServices
<Assembly:InternalsVisibleTo("Friend1, PublicKey=002400000480000094" + _
"0000000602000000240000525341310004000" + _
"001000100bf8c25fcd44838d87e245ab35bf7" + _
"3ba2615707feea295709559b3de903fb95a93" + _
"3d2729967c3184a97d7b84c7547cd87e435b5" + _
"6bdf8621bcb62b59c00c88bd83aa62c4fcdd4" + _
"712da72eec2533dc00f8529c3a0bbb4103282" + _
"f0d894d5f34e9f0103c473dce9f4b457a5dee" + _
"fd8f920d8681ed6dfcb0a81e96bd9b176525a" + _
"26e0b3")>
Public Class FileUtilities
Friend Shared Function AppendDirectorySeparator(dir As String) As String
If Not dir.Trim().EndsWith(Path.DirectorySeparatorChar) Then
Return dir.Trim() + Path.DirectorySeparatorChar
Else
Return dir
End If
End Function
End Class
Jeśli poniższy przykład jest kompilowany w zestawie o silnej nazwie o nazwie Friend1, może pomyślnie wywołać metodę FileUtilities.AppendDirectorySeparator, mimo że metoda jest wewnętrzna dla zestawu Assembly1. Pamiętaj, że jeśli kompilujesz w języku C# z poziomu wiersza polecenia, musisz użyć
//
// The source code should be saved in a file named Friend1.cs. It
// can be compiled at the command line as follows:
//
// csc /r:Assembly1.dll /keyfile:<snkfilename> /out:Friend1.dll Friend1.cs
//
// The public key of the Friend1 assembly should correspond to the public key
// specified in the class constructor of the InternalsVisibleTo attribute in the
// Assembly1 assembly.
//
using System;
public class Example
{
public static void Main()
{
string dir = @"C:\Program Files";
dir = FileUtilities.AppendDirectorySeparator(dir);
Console.WriteLine(dir);
}
}
// The example displays the following output:
// C:\Program Files\
'
' The source code should be saved in a file named Friend1.vb. It
' can be compiled at the command line as follows:
'
' vbc Friend1.vb /r:Assembly1.dll /keyfile:<snkfilename>
'
' The public key of the Friend1 assembly should correspond to the public key
' specified in the class constructor of the InternalsVisibleTo attribute in the
' Assembly1 assembly.
'
Module Example
Public Sub Main()
Dim dir As String = "C:\Program Files"
dir = FileUtilities.AppendDirectorySeparator(dir)
Console.WriteLine(dir)
End Sub
End Module
' The example displays the following output:
' C:\Program Files\
W poniższym przykładzie użyto atrybutu InternalsVisibleToAttribute, aby element członkowski internal niepodpisanego zestawu był widoczny dla innego niepodpisanego zestawu. Atrybut zapewnia, że metoda internalStringLib.IsFirstLetterUpperCase w zestawie o nazwie UtilityLib jest widoczna dla kodu w zestawie o nazwie Friend2. Poniżej znajduje się kod źródłowy UtilityLib.dll:
using System;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleToAttribute("Friend2")]
namespace Utilities.StringUtilities
{
public class StringLib
{
internal static bool IsFirstLetterUpperCase(String s)
{
string first = s.Substring(0, 1);
return first == first.ToUpper();
}
}
}
Imports System.Runtime.CompilerServices
<assembly: InternalsVisibleTo("Friend2")>
Namespace Utilities.StringUtilities
Public Class StringLib
Friend Shared Function IsFirstLetterUpperCase(s As String) As Boolean
Dim first As String = s.Substring(0, 1)
Return first = first.ToUpper()
End Function
End Class
End Namespace
niepodpisane zestawy
Poniższy przykład zawiera kod źródłowy zestawu Friend2. Pamiętaj, że jeśli kompilujesz w języku C# z poziomu wiersza polecenia, musisz użyć
using System;
using Utilities.StringUtilities;
public class Example
{
public static void Main()
{
String s = "The Sign of the Four";
Console.WriteLine(StringLib.IsFirstLetterUpperCase(s));
}
}
Imports Utilities.StringUtilities
Module Example
Public Sub Main()
Dim s As String = "The Sign of the Four"
Console.WriteLine(StringLib.IsFirstLetterUpperCase(s))
End Sub
End Module
Uwagi
Konstruktor InternalsVisibleToAttribute definiuje zestaw przyjazny, który jest zestawem, który ma dostęp do wewnętrznych i prywatnych typów chronionych oraz składowych bieżącego zestawu.
Zarówno obecny zestaw, jak i zestaw znajomy muszą być niepodpisane lub oba muszą być podpisane silną nazwą. (Aby uzyskać więcej informacji na temat zestawów o silnych nazwach, zobacz Create and use strong-named assemblies.) Jeśli oba są niepodpisane, argument assemblyName składa się z nazwy zestawu znajomego, określonego bez ścieżki katalogu lub rozszerzenia pliku. Jeśli oba są podpisane, assemblyName składa się z nazwy zestawu znajomego bez jego ścieżki katalogu lub rozszerzenia nazwy pliku wraz z pełnym kluczem publicznym (ale nie tokenem klucza publicznego). Inne składniki silnej nazwy, takie jak te, które zapewniają informacje o kulturze, wersji lub architekturze procesora, nie można określić w argumencie assemblyName.
Ważny
Jeśli używasz kompilatora języka C# do kompilowania zestawu znajomego, musisz jawnie określić nazwę pliku wyjściowego (.exe lub .dll) przy użyciu opcji /out kompilatora. Jest to wymagane, ponieważ kompilator nie wygenerował jeszcze nazwy zestawu, który kompiluje w momencie powiązania z odwołaniami zewnętrznymi. Opcja kompilatora /out jest opcjonalna dla kompilatora języka Visual Basic, a odpowiedni —out lub -o opcja kompilatora nie powinna być używana podczas kompilowania przyjaznych zestawów za pomocą kompilatora języka F#.
Możesz użyć Sn.exe (narzędzia silnej nazwy), aby pobrać pełny klucz publiczny z pliku klucza o silnej nazwie (snk). W tym celu należy wykonać następujące czynności:
Wyodrębnij klucz publiczny z pliku klucza o silnej nazwie do oddzielnego pliku:
Sn -psnk_filepliku outfile
Wyświetl pełny klucz publiczny w konsoli:
Sn -tppliku outfile
Skopiuj i wklej pełną wartość klucza publicznego do kodu źródłowego.
Aby uzyskać więcej informacji na temat używania atrybutu InternalsVisibleToAttribute, zobacz następujące artykuły:
przyjaznych zestawów (C++) zestawy Friend