語言

InternalsVisibleToAttribute(String) 建構函式

定義

初始化一個新的類別實例 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

一個朋友大會的名字。

範例

簽署大會

以下範例使用屬性 InternalsVisibleToAttribute ,使 internal 一個在有符號的組件中命名 AppendDirectorySeparator 的方法能被另一個已簽署的組件看到。 它定義了一個 FileUtilities 包含內部 AppendDirectorySeparator 方法的類別。 屬性 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 編譯器開關,確保當編譯器綁定外部參考時,友誼組合的名稱仍然可用。

//
// 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 = FileUtilities1.AppendDirectorySeparator(dir)
        Console.WriteLine(dir)
   End Sub
End Module
' The example displays the following output:
'       C:\Program Files\

以下範例利用 屬性 InternalsVisibleToAttribute ,使 internal 未標示組件的成員對另一個未標示組件可見。 屬性確保 internalStringLib.IsFirstLetterUpperCase 名為 的組合語言 UtilityLib 中的方法對名為 Friend2的組合語言中的程式碼可見。 以下是 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 編譯器開關,確保當編譯器綁定外部參考時,友誼組合的名稱仍然可用。

using System;

public class Example1
{
   public static void Main()
   {
      String s = "The Sign of the Four";
      //Console.WriteLine(Utilities.StringUtilities.StringLib.IsFirstLetterUpperCase(s));
   }
}
Module Example1
    Public Sub Main()
        Dim s As String = "The Sign of the Four"
        ' Console.WriteLine(Utilities.StringUtilities.StringLib.IsFirstLetterUpperCase(s))
    End Sub
End Module

備註

InternalsVisibleToAttribute建構子定義了一個友集(friend assembly),即能存取目前集體中內部及私有受保護類型與成員的集合。

現有的集會與友會都必須未簽名,或兩者都必須以強名簽名。 (欲了解更多強命名組件的資訊,請參見 建立並使用強命名組件。)若兩者皆無符號,參數 assemblyName 由友組語言名稱組成,且未指定目錄路徑或檔案副檔名。 若兩者皆有簽名,則 assemblyName 包含友組名稱(不含目錄路徑或檔名副檔名),以及完整的公鑰(但不含公鑰標記)。 強名稱的其他元件,例如提供文化、版本或處理器架構資訊的元件,則無法在參數中 assemblyName 指定。

Important

如果您使用 C# 編譯程式來編譯 friend 元件,則必須使用 /out 編譯程式選項明確指定輸出檔的名稱(.exe 或 .dll)。 這是必要的,因為編譯程式尚未產生它在系結至外部參考時所建置之元件的名稱。 /out 編譯程式選項是 Visual Basic 編譯程式的選擇性選項,而使用 F# 編譯程式編譯 friend 元件時,不應該使用對應的 -out 或 -o 編譯程式選項。

你可以使用 Sn.exe(強命名工具) 從強命名金鑰(.snk)檔案中取得完整的公開金鑰。 若要這麼做,請執行下列步驟:

  1. 將公鑰從強名稱金鑰檔案擷取至個別的檔案:

    Sn -psnk_fileoutfile

  2. 向主控台顯示完整的公鑰:

    Sn -tp輸出檔

  3. 將完整的公鑰值複製並貼到您的原始程式碼中。

欲了解更多如何使用該屬性, InternalsVisibleToAttribute 請參閱以下文章:

適用於