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 assembly that exposes its internal types to this assembly should be
// named Assembly1.dll.
//
// The public key of this assembly should correspond to the public key
// specified in the class constructor of the InternalsVisibleTo attribute in the
// Assembly1 assembly.
//
#using <Assembly1.dll> as_friend

using namespace System;

void main()
{
   String^ dir = L"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.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\

以下示例使用 InternalsVisibleToAttribute 特性使 internal 未签名程序集的成员对另一个无符号程序集可见。 特性可确保 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 编译器开关来确保当编译器绑定到外部引用时友元程序集的名称可用。

#using <UtilityLib.dll> as_friend

using namespace System;
using namespace Utilities::StringUtilities;

void main()
{
   String^ s = "The Sign of the Four";
   Console::WriteLine(StringLib::IsFirstLetterUpperCase(s));
}
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 函数定义友元程序集,该程序集是有权访问当前程序集的内部和专用受保护类型和成员的程序集。

当前程序集和友元程序集都必须是无符号的,或者两者都必须使用强名称进行签名。 (有关强名称程序集的详细信息,请参阅Create和使用强名称程序集。) 如果两者都是无符号的,则assemblyName参数由未指定目录路径或文件扩展名的友元程序集的名称组成。 如果两者都已签名, assemblyName 则包含友元程序集的名称(不含其目录路径或文件扩展名),以及其完整的公钥 (,但不包括其公钥令牌) 。 无法在 参数中 assemblyName 指定强名称的其他组件,例如提供区域性、版本或处理器体系结构信息的组件。

重要

如果使用 C# 编译器编译友元程序集,则必须使用 /out 编译器选项显式指定输出文件的名称 (.exe 或 .dll) 。 这是必需的,因为编译器尚未为它在绑定到外部引用时而正在构建的程序集生成名称。 /out 编译器选项对于 Visual Basic 编译器是可选的,使用 F# 编译器编译友元程序集时,不应使用相应的 -out-o 编译器选项。

可以使用 Sn.exe (强名称工具) 从强名称密钥 (.snk) 文件检索完整的公钥。 为此,请执行以下步骤:

  1. 将公钥从强名称密钥文件提取到单独的文件:

    Sn -psnk_fileoutfile

  2. 向控制台显示完整的公钥:

    Sn -tpoutfile

  3. 将完整的公钥值复制并粘贴到源代码中。

有关如何使用 InternalsVisibleToAttribute 特性的详细信息,请参阅以下文章:

适用于