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 使用して、 internal 署名付きアセンブリ内の という名前 AppendDirectorySeparator のメソッドを別の署名済みアセンブリに表示します。 内部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 、署名されていないアセンブリのメンバーを別の署名されていないアセンブリに表示します。 属性を使用すると、 という名前の internalStringLib.IsFirstLetterUpperCase アセンブリ内のメソッドが、 という UtilityLib 名前 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 フレンド アセンブリを定義します。これは、現在のアセンブリの内部およびプライベートで保護された型とメンバーにアクセスできるアセンブリです。

現在のアセンブリとフレンド アセンブリの両方が符号なしであるか、両方とも厳密な名前で署名されている必要があります。 (厳密な名前付きアセンブリの詳細については、「厳密な名前付きアセンブリをCreateして使用する」を参照してください)。両方が符号なしの場合、assemblyName引数は、ディレクトリ パスまたはファイル拡張子なしで指定されたフレンド アセンブリの名前で構成されます。 両方が署名されている場合は、 assemblyName ディレクトリ パスまたはファイル名拡張子のないフレンド アセンブリの名前と、完全な公開キー (公開キー トークンではなく) で構成されます。 厳密な名前の他のコンポーネント (カルチャ、バージョン、プロセッサ アーキテクチャ情報を提供するものなど) は、 引数で assemblyName 指定できません。

重要

C# コンパイラを使用してフレンド アセンブリをコンパイルする場合は、 /out コンパイラ オプションを使用して出力ファイルの名前 (.exe または .dll) を明示的に指定する必要があります。 この指定は必ず行ってください。コンパイラが外部参照にバインドする時点ではまだ、ビルド中のアセンブリの名前が生成されていないためです。 Visual Basic コンパイラでは /out コンパイラ オプションは省略可能であり、F# コンパイラを使用してフレンド アセンブリをコンパイルする場合は、対応する -out または -o コンパイラ オプションを使用しないでください。

Sn.exe (厳密な名前ツール) を使用して、厳密な名前付きキー (.snk) ファイルから完全な公開キーを取得できます。 これを行うには、次の手順を実行します。

  1. 厳密な名前付きキー ファイルから別のファイルに公開キーを抽出します。

    Sn -psnk_fileoutfile

  2. 完全な公開キーをコンソールに表示します。

    Sn -tpoutfile

  3. 完全な公開キーの値をコピーしてソース コードに貼り付けます。

属性の使用方法 InternalsVisibleToAttribute の詳細については、次の記事を参照してください。

適用対象