InternalsVisibleToAttribute(String) 생성자
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
지정된 friend 어셈블리의 InternalsVisibleToAttribute 이름을 사용하여 클래스의 새 인스턴스를 초기화합니다.
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)
매개 변수
- assemblyName
- String
friend 어셈블리의 이름입니다.
예제
서명된 어셈블리
다음 예제에서는 특성을 사용하여 InternalsVisibleToAttribute 서명된 internal 어셈블리에 명명 AppendDirectorySeparator 된 메서드를 다른 서명된 어셈블리에 표시합니다. 내부 AppendDirectorySeparator 메서드를 FileUtilities 포함하는 클래스를 정의합니다. 특성은 InternalsVisibleToAttribute 클래스를 포함하는 어셈블리에 FileUtilities 적용됩니다. 이 특성을 사용하면 명명 Friend1 된 어셈블리가 이 내부 멤버에 액세스할 수 있습니다.
//
// 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
다음 예제가 강력한 이름의 Friend1어셈블리로 컴파일되는 경우 메서드가 어셈블리 내부임에도 불구하고 메서드를 성공적으로 호출 FileUtilities.AppendDirectorySeparator 할 Assembly1 수 있습니다. 명령줄에서 C#으로 컴파일하는 경우 /out 컴파일러 스위치를 사용하여 컴파일러가 외부 참조에 바인딩할 때 friend 어셈블리의 이름을 사용할 수 있는지 확인해야 합니다.
//
// 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\
다음 예제에서는 특성을 internal 사용하여 InternalsVisibleToAttribute 서명되지 않은 어셈블리의 멤버를 서명되지 않은 다른 어셈블리에 표시합니다. 이 특성은 명명된 어셈블리의 internalStringLib.IsFirstLetterUpperCase 메서드가 명명 UtilityLibFriend2된 어셈블리의 코드에 표시되는지 확인합니다. 다음은 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
서명되지 않은 어셈블리
다음 예제에서는 어셈블리에 대한 Friend2 소스 코드를 제공합니다. 명령줄에서 C#으로 컴파일하는 경우 /out 컴파일러 스위치를 사용하여 컴파일러가 외부 참조에 바인딩할 때 friend 어셈블리의 이름을 사용할 수 있는지 확인해야 합니다.
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
설명
InternalsVisibleToAttribute 생성자는 현재 어셈블리의 내부 및 프라이빗 보호 형식 및 멤버에 액세스할 수 있는 어셈블리인 friend 어셈블리를 정의합니다.
현재 어셈블리와 friend 어셈블리는 모두 서명되지 않아야 합니다. 또는 둘 다 강력한 이름으로 서명해야 합니다. 강력한 이름의 어셈블리에 대한 자세한 내용은 강력한 이름의 어셈블리 만들기 및 사용을 참조하세요. 둘 다 서명 assemblyName 되지 않은 경우 인수는 디렉터리 경로 또는 파일 확장명 없이 지정된 friend 어셈블리의 이름으로 구성됩니다. 둘 다 서명 assemblyName 된 경우 디렉터리 경로 또는 파일 이름 확장명 없이 friend 어셈블리의 이름과 전체 공개 키(공개 키 토큰은 아님)로 구성됩니다. 문화권, 버전 또는 프로세서 아키텍처 정보를 제공하는 것과 같은 강력한 이름의 다른 구성 요소는 인수에 assemblyName 지정할 수 없습니다.
Important
C# 컴파일러를 사용하여 friend 어셈블리를 컴파일하는 경우 /out 컴파일러 옵션을 사용하여 출력 파일(.exe 또는 .dll)의 이름을 명시적으로 지정해야 합니다. 컴파일러가 외부 참조에 바인딩할 때 빌드 중인 어셈블리의 이름을 아직 생성하지 않았기 때문에 이 작업이 필요합니다. /out 컴파일러 옵션은 Visual Basic 컴파일러에 대한 선택 사항이며 F# 컴파일러를 사용하여 friend 어셈블리를 컴파일할 때는 해당 -out 또는 -o 컴파일러 옵션을 사용하면 안 됩니다.
Sn.exe(강력한 이름 도구)를 사용하여 강력한 이름의 키(.snk) 파일에서 전체 공개 키를 검색할 수 있습니다. 이렇게 하려면 다음 단계를 수행합니다.
강력한 이름의 키 파일에서 공개 키를 별도의 파일로 추출합니다.
Sn -psnk_file아웃파일
콘솔에 전체 공개 키를 표시합니다.
Sn -tpoutfile
전체 공개 키 값을 복사하여 소스 코드에 붙여넣습니다.
특성을 사용하는 InternalsVisibleToAttribute 방법에 대한 자세한 내용은 다음 문서를 참조하세요.