InternalsVisibleToAttribute クラス

定義

現在のアセンブリ内でのみ通常表示される型が、指定したアセンブリに対して表示されるように指定します。

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
継承
InternalsVisibleToAttribute
属性

署名付きアセンブリ

次の例では、InternalsVisibleToAttribute 属性を使用して、署名されたアセンブリ内の AppendDirectorySeparator という名前の internal メソッドを別の署名済みアセンブリから参照できるようにしています。 内部 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という名前の厳密な名前のアセンブリにコンパイルされている場合、Friend1Example.Main メソッドは、Assembly1 アセンブリの内部にありますが、FileUtilities.AppendDirectorySeparator メソッドを正常に呼び出すことができます。 コマンド ラインから 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

注釈

この 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)

適用対象