InternalsVisibleToAttribute 类

定义

指定通常仅在当前程序集内可见的类型对指定的程序集可见。

C#
[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=true, Inherited=false)]
public sealed class InternalsVisibleToAttribute : Attribute
继承
InternalsVisibleToAttribute
属性

示例

签名程序集

以下示例使用 InternalsVisibleToAttribute 属性在对另一个已签名程序集可见的已签名程序集中创建名为 AppendDirectorySeparatorinternal 方法。 它定义包含内部 AppendDirectorySeparator 方法的 FileUtilities 类。 InternalsVisibleToAttribute 属性应用于包含 FileUtilities 类的程序集。 该特性允许名为 Friend1 的程序集访问此内部成员。

C#
//
// 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;
   }
}

如果以下示例编译为名为 Friend1的强名称程序集,Friend1 中的 Example.Main 方法可以成功调用 FileUtilities.AppendDirectorySeparator 方法,尽管该方法在 Assembly1 程序集内部。 请注意,如果要从命令行在 C# 中编译,则必须使用 /out 编译器开关来确保编译器绑定到外部引用时,友元程序集的名称可用。

C#
//
// 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\

未签名程序集

以下示例使用 InternalsVisibleToAttribute 属性使未签名程序集 internal 成员对另一个未签名程序集可见。 该特性可确保名为 UtilityLib 的程序集中的 internalStringLib.IsFirstLetterUpperCase 方法对名为 Friend2的程序集中的代码可见。 下面是 UtilityLib.dll的源代码:

C#
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();
      }
   }
}

以下示例提供 Friend2 程序集的源代码。 请注意,如果要从命令行在 C# 中编译,则必须使用 /out 编译器开关来确保编译器绑定到外部引用时,友元程序集的名称可用。

C#
using System;
using Utilities.StringUtilities;

public class Example
{
   public static void Main()
   {
      String s = "The Sign of the Four";
      Console.WriteLine(StringLib.IsFirstLetterUpperCase(s));
   }
}

注解

有关此 API 的详细信息,请参阅 InternalsVisibleToAttribute的补充 API 备注。

构造函数

InternalsVisibleToAttribute(String)

使用指定友元程序集的名称初始化 InternalsVisibleToAttribute 类的新实例。

属性

AllInternalsVisible

此属性未实现。

AssemblyName

获取使用 internal 关键字标记的所有类型和类型成员的友元程序集的名称。

TypeId

在派生类中实现时,获取此 Attribute的唯一标识符。

(继承自 Attribute)

方法

Equals(Object)

返回一个值,该值指示此实例是否等于指定对象。

(继承自 Attribute)
GetHashCode()

返回此实例的哈希代码。

(继承自 Attribute)
GetType()

获取当前实例的 Type

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否为派生类的默认值。

(继承自 Attribute)
Match(Object)

在派生类中重写时,返回一个值,该值指示此实例是否等于指定对象。

(继承自 Attribute)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

显式接口实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射到相应的调度标识符集。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,该信息可用于获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口数(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

产品 版本
.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