InternalsVisibleToAttribute(String) 构造函数
定义
用指定的友元程序集的名称初始化 InternalsVisibleToAttribute 类的新实例。Initializes a new instance of the InternalsVisibleToAttribute class with the name of the specified friend assembly.
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
友元程序集的名称。The name of a friend assembly.
示例
签名的程序集Signed assemblies
下面的示例使用 InternalsVisibleToAttribute 属性,使 internal 签名程序集中名为的方法 AppendDirectorySeparator 对其他已签名程序集可见。The following example uses the InternalsVisibleToAttribute attribute to make an internal method named AppendDirectorySeparator in a signed assembly visible to another signed assembly. 它定义了一个 FileUtilities 包含内部方法的类 AppendDirectorySeparator 。It defines a FileUtilities class that includes an internal AppendDirectorySeparator method. InternalsVisibleToAttribute特性应用于包含类的程序集 FileUtilities 。The InternalsVisibleToAttribute attribute is applied to the assembly that contains the FileUtilities class. 特性允许名为的程序集 Friend1 访问此内部成员。The attribute allows an assembly named Friend1 to access this internal member.
//
// 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 。If the following example is compiled into a strong-named assembly named Friend1, it can successfully call the FileUtilities.AppendDirectorySeparator method, even though the method is internal to the Assembly1 assembly. 请注意,如果从命令行在 c # 中进行编译,则必须使用 /out 编译器开关,以确保当编译器绑定到外部引用时,友元程序集的名称可用。Note that if you are compiling in C# from the command line, you must use the /out compiler switch to ensure that the name of the friend assembly is available when the compiler binds to external references.
//
// 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 无符号程序集的成员对其他未签名的程序集可见。The following example uses the InternalsVisibleToAttribute attribute to make an internal member of an unsigned assembly visible to another unsigned assembly. 特性可确保名为的程序集中的代码对名为的程序集中的 internal StringLib.IsFirstLetterUpperCase 方法 UtilityLib 可见 Friend2 。The attribute ensures that the internal StringLib.IsFirstLetterUpperCase method in an assembly named UtilityLib is visible to the code in an assembly named Friend2. 下面是 UtilityLib.dll 的源代码:The following is the source code for 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
未签名的程序集Unsigned assemblies
下面的示例提供 Friend2 程序集的源代码。The following example provides the source code for the Friend2 assembly. 请注意,如果从命令行在 c # 中进行编译,则必须使用 /out 编译器开关,以确保当编译器绑定到外部引用时,友元程序集的名称可用。Note that if you are compiling in C# from the command line, you must use the /out compiler switch to ensure that the name of the friend assembly is available when the compiler binds to external references.
#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构造函数定义友元程序集,该程序集是有权访问当前程序集的内部和私有受保护类型和成员的程序集。The InternalsVisibleToAttribute constructor defines a friend assembly, which is an assembly that has access to the internal and private protected types and members of the current assembly.
当前程序集和友元程序集都必须是无符号的,或者两者都必须使用强名称进行签名。Both the current assembly and the friend assembly must be unsigned, or both must be signed with a strong name. (有关强名称程序集的详细信息,请参阅 创建和使用具有强名称的程序集) 。如果这两种方法都无符号,则该 assemblyName 参数由指定的友元程序集的名称组成,无需使用目录路径或文件扩展名。(For more information about strong-named assemblies, see Create and use strong-named assemblies.) If both are unsigned, the assemblyName argument consists of the name of the friend assembly, specified without a directory path or file extension. 如果两个都有符号,则 assemblyName 包含友元程序集的名称,而不包含其目录路径或文件扩展名,以及其完整公钥 (但不) 其公钥标记。If both are signed, assemblyName consists of the name of the friend assembly without its directory path or file name extension, along with its full public key (but not its public key token). 不能在参数中指定强名称的其他组件,如提供区域性、版本或处理器体系结构信息的组件 assemblyName 。The other components of a strong name, such as those that provide culture, version, or processor architecture information, cannot be specified in the assemblyName argument.
重要
如果使用 c # 编译器编译 friend 程序集,则必须使用 /out 编译器选项显式指定输出文件 ( 或 .dll) 的名称。If you use the C# compiler to compile the friend assembly, you must explicitly specify the name of the output file (.exe or .dll) by using the /out compiler option. 这是必需的,因为编译器尚未为它在绑定到外部引用时而正在构建的程序集生成名称。This is required because the compiler has not yet generated the name for the assembly it is building at the time it is binding to external references. /Out 编译器选项对于 Visual Basic 编译器是可选的,并且不应在使用 F # 编译器编译友元程序集时使用相应的 输出 或 -o 编译器选项。The /out compiler option is optional for the Visual Basic compiler, and the corresponding -out or -o compiler option should not be used when compiling friend assemblies with the F# compiler.
您可以使用 Sn.exe (强名称工具) 从具有强名称的密钥 ( .snk) 文件中检索完整公钥。You can use Sn.exe (Strong Name Tool) to retrieve the full public key from a strong-named key (.snk) file. 为此,请执行以下步骤:To do this, you perform the following steps:
将公钥从强名称密钥文件提取到单独的文件中:Extract the public key from the strong-named key file to a separate file:
Sn-p snk_file outfileSn -p snk_file outfile
向控制台显示完整公钥:Display the full public key to the console:
Sn-tp outfileSn -tp outfile
将完整的公钥值复制并粘贴到源代码中。Copy and paste the full public key value into your source code.
有关如何使用属性的详细信息 InternalsVisibleToAttribute ,请参阅以下主题:For more information about how to use the InternalsVisibleToAttribute attribute, see the following topics: