InternalsVisibleToAttribute クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
通常は現在のアセンブリ内でのみ参照できる型が、指定したアセンブリから参照可能であることを指定します。
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 使用して、 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
きの厳密な名前付きアセンブリにコンパイルされている場合、Example.Main
メソッドはアセンブリのFriend1
内部ですが、メソッドを正常に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
符号なしアセンブリのメンバーを別の符号なしアセンブリに表示します。 この属性により、internal``StringLib.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
注釈
通常、C# のスコープを持つinternal
型とメンバー、またはFriend
Visual Basic内のスコープは、定義されているアセンブリでのみ表示されます。 スコープ (Protected Friend
Visual Basic内のスコープ) を持つprotected internal
型とメンバーは、独自のアセンブリ内、または包含クラスから派生した型にのみ表示されます。 スコープ (Private Protected
Visual Basic内のスコープ) を持つprivate protected
型とメンバーは、現在のアセンブリ内の包含クラスから派生した包含クラスまたは型で表示されます
この属性により InternalsVisibleToAttribute 、これらの型とメンバーは、指定したアセンブリ内の型 (フレンド アセンブリとも呼ばれます) にも表示されます。 これは、(Visual Basic)protected internal
、(Protected Friend
Visual Basic)、および private protected
(Private Protected
Visual Basic) メンバーにのみinternal
適用されますが、メンバーには適用されませんprivate
。Friend
注意
(Visual Basic) メンバーのprivate protected
場合、属性は、メンバーのInternalsVisibleToAttribute包含クラス から派生する型にのみアクセシビリティを拡張します。Private Protected
属性はアセンブリ レベルで適用されます。 つまり、ソース コード ファイルの先頭に含めることができるか、Visual Studio プロジェクトの AssemblyInfo ファイルに含めることができます。 この属性を使用して、現在のアセンブリの内部型とメンバーにアクセスできる 1 つのフレンド アセンブリを指定できます。 複数のフレンド アセンブリは、2 つの方法で定義できます。 次の例に示すように、個々のアセンブリ レベルの属性として表示できます。
[assembly:InternalsVisibleTo("Friend1a")]
[assembly:InternalsVisibleTo("Friend1b")]
<assembly:InternalsVisibleTo("Friend1a")>
<assembly:InternalsVisibleTo("Friend1b")>
また、次の例に示すように、個別 InternalsVisibleToAttribute のタグを使用して 1 つの assembly
キーワードで表示することもできます。
[assembly:InternalsVisibleTo("Friend2a"),
InternalsVisibleTo("Friend2b")]
<Assembly:InternalsVisibleTo("Friend2a"), _
Assembly:InternalsVisibleTo("Friend2b")>
フレンド アセンブリは、コンストラクターによって InternalsVisibleToAttribute 識別されます。 現在のアセンブリとフレンド アセンブリの両方が符号なしであるか、両方のアセンブリが厳密な名前で署名されている必要があります。
両方のアセンブリが符号なしの場合、 assemblyName
引数はフレンド アセンブリの名前で構成され、ディレクトリ パスまたはファイル名拡張子なしで指定されます。
両方のアセンブリが厳密な名前で署名されている場合、コンストラクターへの InternalsVisibleToAttribute 引数は、ディレクトリ パスまたはファイル名拡張子を持たないアセンブリの名前と、完全な公開キー (公開キー トークンではなく) で構成されている必要があります。 厳密な名前付きアセンブリの完全な公開キーを取得するには、この記事の後半の 「完全な公開キーの取得 」セクションを参照してください。 厳密な名前付きアセンブリでの使用 InternalsVisibleToAttribute の詳細については、コンストラクターを InternalsVisibleToAttribute 参照してください。
引数に 、VersionまたはProcessorArchitectureフィールドの値をCultureInfo含めないでください。Visual Basic、C#、および C++ コンパイラはこれをコンパイラ エラーとして扱います。 エラーとして扱わないコンパイラ ( IL アセンブラー (ILAsm.exe) など) を使用し、アセンブリが厳密な名前を持つ場合、 MethodAccessException 指定したフレンド アセンブリが属性を含む InternalsVisibleToAttribute アセンブリに初めてアクセスすると、例外がスローされます。
この属性の使用方法の詳細については、「 フレンド アセンブリ と C++ フレンド アセンブリ」を参照してください。
完全な公開キーを取得する
厳密な名前ツール (Sn.exe) を使用して、厳密な名前付きキー (.snk) ファイルから完全な公開キーを取得できます。 これを行うには、次の手順を実行します。
厳密な名前付きキー ファイルから別のファイルに公開キーを抽出します。
Sn -p snk_file outfile
コンソールに完全な公開キーを表示します。
Sn - tpoutfile
完全な公開キーの値をコピーしてソース コードに貼り付けます。
C を使用してフレンド アセンブリをコンパイルする#
C# コンパイラを使用してフレンド アセンブリをコンパイルする場合は、 /out コンパイラ オプションを使用して出力ファイルの名前 (.exeまたは.dll) を明示的に指定する必要があります。 この指定は必ず行ってください。コンパイラが外部参照にバインドする時点ではまだ、ビルド中のアセンブリの名前が生成されていないためです。 /out コンパイラ オプションは、Visual Basic コンパイラでは省略可能であり、F# コンパイラでフレンド アセンブリをコンパイルするときは、対応する -out または -o コンパイラ オプションを使用しないでください。
C++ を使用してフレンド アセンブリをコンパイルする
C++ では、属性によって有効になっている内部メンバーを InternalsVisibleToAttribute フレンド アセンブリからアクセスできるようにするには、C++ ディレクティブで属性を as_friend
使用する必要があります。 詳細は、フレンド アセンブリ (C++) を参照してください。
コンストラクター
InternalsVisibleToAttribute(String) |
指定したフレンド アセンブリの名前を使用して InternalsVisibleToAttribute クラスの新しいインスタンスを初期化します。 |
プロパティ
AllInternalsVisible |
このプロパティは実装されていません。 |
AssemblyName |
|
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) |