InternalsVisibleToAttribute 类
定义
指定通常仅在当前程序集中可见的类型对指定程序集可见。Specifies that types that are ordinarily visible only within the current assembly are visible to a specified assembly.
public ref class InternalsVisibleToAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=true, Inherited=false)]
public sealed class InternalsVisibleToAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=true, Inherited=false)>]
type InternalsVisibleToAttribute = class
inherit Attribute
Public NotInheritable Class InternalsVisibleToAttribute
Inherits Attribute
- 继承
- 属性
示例
签名的程序集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 ,则 Example.Main 中的方法 Friend1 可以成功调用 FileUtilities.AppendDirectorySeparator 方法,尽管该方法是程序集的内部方法 Assembly1 。If the following example is compiled into a strong-named assembly named Friend1, the Example.Main method in Friend1 can successfully call the FileUtilities.AppendDirectorySeparator method, although 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\
未签名的程序集Unsigned assemblies
下面的示例使用 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
下面的示例提供 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
注解
通常情况下,c # 中具有或范围 (的类型和成员 internal private protected ) 和中的作用域 Friend Private Protected (Visual Basic) 仅在定义它们的程序集中可见。Ordinarily, types and members with internal or private protected scope (in C#) and Friend and Private Protected scope (in Visual Basic) are visible only in the assembly in which they are defined. InternalsVisibleToAttribute特性使它们也对指定程序集中的类型可见,这称为友元程序集。The InternalsVisibleToAttribute attribute makes them also visible to the types in a specified assembly, which is known as a friend assembly. 这仅适用于 vb 中的 internal (Friend) 或仅适用于 private protected Private Protected vb) 方法中的 (,而不 private 适用于其中的项。This only applies to internal (Friend in VB) or private protected(Private Protected in VB) methods only, but not private ones.
在程序集级别应用特性。The attribute is applied at the assembly level. 这意味着它可以包括在源代码文件的开头,也可以包含在 Visual Studio 项目的 AssemblyInfo 文件中。This means that it can be included at the beginning of a source code file, or it can be included in the AssemblyInfo file in a Visual Studio project. 您可以使用特性来指定可访问当前程序集的内部类型和成员的单个友元程序集。You can use the attribute to specify a single friend assembly that can access the internal types and members of the current assembly. 可以通过两种方式来定义多个友元程序集。You can define multiple friend assemblies in two ways. 它们可以显示为单独的程序集级别特性,如下面的示例所示。They can appear as individual assembly-level attributes, as the following example illustrates.
[assembly:InternalsVisibleTo("Friend1a")]
[assembly:InternalsVisibleTo("Friend1b")]
<assembly:InternalsVisibleTo("Friend1a")>
<assembly:InternalsVisibleTo("Friend1b")>
它们还可以用单独的 InternalsVisibleToAttribute 标记显示,但使用单个 assembly 关键字,如下面的示例所示。They can also appear with separate InternalsVisibleToAttribute tags but a single assembly keyword, as the following example illustrates.
[assembly:InternalsVisibleTo("Friend2a"),
InternalsVisibleTo("Friend2b")]
<Assembly:InternalsVisibleTo("Friend2a"), _
Assembly:InternalsVisibleTo("Friend2b")>
友元程序集由 InternalsVisibleToAttribute 构造函数标识。The friend assembly is identified by the InternalsVisibleToAttribute constructor. 当前程序集和友元程序集都必须是无符号的,或者这两个程序集都必须使用强名称进行签名。Both the current assembly and the friend assembly must be unsigned, or both assemblies must be signed with a strong name.
如果这两个程序集都无符号,则 assemblyName 参数由指定的友元程序集的名称组成,无需使用目录路径或文件扩展名。If both assemblies are unsigned, the assemblyName argument consists of the name of the friend assembly, specified without a directory path or file name extension.
如果这两个程序集都使用强名称进行签名,则 InternalsVisibleToAttribute 构造函数的参数必须包含程序集的名称,而不包含其目录路径或文件扩展名,以及完整的公钥 (而不是其公钥标记) 。If both assemblies are signed with a strong name, the argument to the InternalsVisibleToAttribute constructor must consist of the name of the assembly without its directory path or file name extension, along with the full public key (and not its public key token). 若要获取强名称程序集的完整公钥,请参阅本文后面的 获取完整公钥 部分。To get the full public key of a strong-named assembly, see the Getting the full public key section later in this article. 有关使用 InternalsVisibleToAttribute 具有强名称的程序集的详细信息,请参阅 InternalsVisibleToAttribute 构造函数。For more information about using InternalsVisibleToAttribute with strong-named assemblies, see the InternalsVisibleToAttribute constructor.
不要 CultureInfo Version 在参数中包含、或字段的值, ProcessorArchitecture Visual Basic、c # 和 c + + 编译器将其视为编译器错误。Do not include values for the CultureInfo, Version, or ProcessorArchitecture field in the argument; the Visual Basic, C#, and C++ compilers treat this as a compiler error. 如果你使用的编译器不会将其视为错误 (例如 IL 汇编程序 (ILAsm.exe) ) 并且程序集具有强名称,则 MethodAccessException 在指定的友元程序首次访问包含该特性的程序集时,将引发异常 InternalsVisibleToAttribute 。If you use a compiler that does not treat it as an error (such as the IL Assembler (ILAsm.exe)) and the assemblies are strong-named, a MethodAccessException exception is thrown the first time the specified friend assembly accesses the assembly that contains the InternalsVisibleToAttribute attribute.
有关如何使用此属性的详细信息,请参阅以下主题:For more information about how to use this attribute, see the following topics:
获取完整公钥Getting the full public key
您可以使用 强名称工具 (Sn.exe) 从强名称密钥 ( .snk) 文件中检索完整公钥。You can use the Strong Name Tool (Sn.exe) 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.
用 C 编译友元程序集#Compiling the friend assembly with C#
如果使用 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.
用 c + + 编译友元程序集Compiling the friend assembly with C++
在 c + + 中,若要使特性启用的内部成员可供 InternalsVisibleToAttribute 友元程序集访问,则必须使用 as_friend c + + 指令中的特性。In C++, in order to make the internal members enabled by the InternalsVisibleToAttribute attribute accessible to a friend assembly, you must use the as_friend attribute in the C++ directive. 有关详细信息,请参阅 友元程序集 (c + +) 。For more information, see Friend Assemblies (C++).
构造函数
| InternalsVisibleToAttribute(String) |
用指定的友元程序集的名称初始化 InternalsVisibleToAttribute 类的新实例。Initializes a new instance of the InternalsVisibleToAttribute class with the name of the specified friend assembly. |
属性
| AllInternalsVisible |
不实现此属性。This property is not implemented. |
| AssemblyName |
获取友元程序集的名称,采用 |
| TypeId |
在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute. (继承自 Attribute) |
方法
| Equals(Object) |
返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object. (继承自 Attribute) |
| GetHashCode() |
返回此实例的哈希代码。Returns the hash code for this instance. (继承自 Attribute) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| IsDefaultAttribute() |
在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (继承自 Attribute) |
| Match(Object) |
当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (继承自 Attribute) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers. (继承自 Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface. (继承自 Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1). (继承自 Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object. (继承自 Attribute) |