使用英语阅读

通过


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 属性在对另一个已签名程序集可见的已签名程序集中创建名为 AppendDirectorySeparatorinternal 方法。 它定义包含内部 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 编译器开关来确保编译器绑定到外部引用时,友元程序集的名称可用。

//
// 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 成员对另一个未签名程序集可见。 该特性可确保名为 UtilityLib 的程序集中的 internalStringLib.IsFirstLetterUpperCase 方法对名为 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 <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 构造函数定义友元程序集,该程序集是有权访问当前程序集的内部和私有受保护类型和成员的程序集。

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

重要

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

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

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

    Sn -psnk_file文件

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

    Sn -tp文件

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

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

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0