このフィールドに、 FamilyOrAssembly レベルの参照可能範囲が設定されているかどうかを示す値を取得します。
Public ReadOnly Property IsFamilyOrAssembly As Boolean
[C#]
public bool IsFamilyOrAssembly {get;}
[C++]
public: __property bool get_IsFamilyOrAssembly();
[JScript]
public function get IsFamilyOrAssembly() : Boolean;
プロパティ値
フィールドに FamORAssem 属性が設定されている場合は true 。それ以外の場合は false 。
解説
フィールドに FamilyOrAssembly レベルの参照可能範囲が設定されている場合、そのフィールドは派生クラスのすべてのメンバ、または同じアセンブリ内の任意のメンバから呼び出せますが、他の型からは呼び出せません。
IsFamilyOrAssembly プロパティは、 FieldAttributes.FamORAssem 属性が設定されたときに設定されます。
使用例
[Visual Basic, C#, C++] 2 つのフィールドを作成する例を次に示します。2 番目のフィールドである Myfieldb は、最初のフィールド Myfielda から派生しています。Myfielda.field は protected internal (Visual Basic の場合は Friend Protected) であるため、Myfieldb.field を派生できます。Myfielda.field がプライベート フィールドの場合は、Myfieldb.field を派生できません。
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic
' Make two fields.
Public Class Myfielda
' Note that if the private line below is uncommented
' and the protected internal line below is commented out,
' this will not compile, because Myfielda.field is inaccessible.
' private string field = "A private field";
Protected Friend field As String = "A private field"
Public Property MyField() As String
Get
Return field
End Get
Set(ByVal Value As String)
If field <> value Then
field = value
End If
End Set
End Property
End Class 'Myfielda
' Myfieldb is derived from Myfielda.
' The protected internal string field allows
' the IsFamilyOrAssembly flag to be set and allows
' the field to be visible from a derived class.
Public Class Myfieldb
Inherits Myfielda
Public Shadows Property MyField() As String
Get
Return field
End Get
Set(ByVal Value As String)
If field <> value Then
field = value
End If
End Set
End Property
End Class 'Myfieldb
Public Class Myfieldinfo
Public Shared Function Main() As Integer
Console.WriteLine("Reflection.FieldInfo")
Dim Myfielda As New Myfielda()
Dim Myfieldb As New Myfieldb()
' Get the Type and FieldInfo.
Dim MyTypea As Type = Type.GetType("Myfielda")
Dim Myfieldinfoa As FieldInfo = MyTypea.GetField("field", BindingFlags.NonPublic Or BindingFlags.Instance)
Dim MyTypeb As Type = Type.GetType("Myfieldb")
Dim Myfieldinfob As FieldInfo = MyTypeb.GetField("field", BindingFlags.NonPublic Or BindingFlags.Instance)
' For the first field, get and display the Name, field, and
' IsFamilyOrAssembly.
Console.WriteLine("{0} - {1}", MyTypea.FullName, Myfieldinfoa.GetValue(Myfielda))
Console.WriteLine("IsFamilyOrAssembly = {0}", Myfieldinfoa.IsFamilyOrAssembly)
If Myfieldinfoa.IsFamilyOrAssembly Then
Console.WriteLine("Field has limited accessibility")
End If
' For the second field, get and display the name and field.
Console.WriteLine("{0} - {1}", MyTypeb.FullName, Myfieldinfob.GetValue(Myfieldb))
Return 0
End Function 'Main
End Class 'Myfieldinfo
[C#]
using System;
using System.Reflection;
//Make two fields.
public class Myfielda
// Note that if the private line below is uncommented
// and the protected internal line below is commented out,
// this will not compile, because Myfielda.field is inaccessible.
{
// private string field = "A private field";
protected internal string field = "A private field";
public string Field
{
get{return field;}
set{if(field!=value) {field=value;}}
}
}
// Myfieldb is derived from Myfielda.
// The protected internal string field allows
// the IsFamilyOrAssembly flag to be set and allows
// the field to be visible from a derived class.
public class Myfieldb:Myfielda
{
new public string Field
{
get{return field;}
set{if(field!=value){field=value;}}
}
}
public class Myfieldinfo
{
public static int Main()
{
Console.WriteLine("\nReflection.FieldInfo");
Myfielda Myfielda = new Myfielda();
Myfieldb Myfieldb = new Myfieldb();
// Get the Type and FieldInfo.
Type MyTypea = Type.GetType("Myfielda");
FieldInfo Myfieldinfoa = MyTypea.GetField("field",
BindingFlags.NonPublic|BindingFlags.Instance);
Type MyTypeb = Type.GetType("Myfieldb");
FieldInfo Myfieldinfob = MyTypeb.GetField("field",
BindingFlags.NonPublic|BindingFlags.Instance);
// For the first field, get and display the Name, field, and
// IsFamilyOrAssembly.
Console.WriteLine("\n{0} - ", MyTypea.FullName);
Console.WriteLine("{0};", Myfieldinfoa.GetValue(Myfielda));
Console.WriteLine("IsFamilyOrAssembly = {0};",
Myfieldinfoa.IsFamilyOrAssembly);
if (Myfieldinfoa.IsFamilyOrAssembly )
Console.WriteLine("Field has limited accessibility");
// For the second field, get and display the name and field.
Console.WriteLine("\n{0} - ", MyTypeb.FullName);
Console.WriteLine("{0}", Myfieldinfob.GetValue(Myfieldb));
return 0;
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;
//Make two fields.
public __gc class Myfielda
// Note that if the private: line below is uncommented
// and the public protected: line below is commented out,
// this will not compile, because Myfielda.field is inaccessible.
{
// private:
public protected:
String* field;
public:
Myfielda() : field(S"A private field") {}
__property String* get_Field() {
return field;
}
__property void set_Field(String* value) {
if(field!=value) {
field=value;
}
}
};
// Myfieldb is derived from Myfielda.
// The protected internal string field allows
// the IsFamilyOrAssembly flag to be set and allows
// the field to be visible from a derived class.
public __gc class Myfieldb:public Myfielda
{
public:
__property String* get_Field() {
return field;
}
__property void set_Field(String* value) {
if(field!=value){
field=value;
}
}
};
int main()
{
Console::WriteLine(S"\nReflection.FieldInfo");
Myfielda* myfielda = new Myfielda();
Myfieldb* myfieldb = new Myfieldb();
// Get the Type and FieldInfo.
Type* MyTypea = Type::GetType(S"Myfielda");
FieldInfo* Myfieldinfoa = MyTypea->GetField(S"field",
static_cast<BindingFlags>(BindingFlags::NonPublic|BindingFlags::Instance));
Type* MyTypeb = Type::GetType(S"Myfieldb");
FieldInfo* Myfieldinfob = MyTypeb->GetField(S"field",
static_cast<BindingFlags>(BindingFlags::NonPublic|BindingFlags::Instance));
// For the first field, get and display the Name, field, and
// IsFamilyOrAssembly.
Console::WriteLine(S"\n{0} - ", MyTypea->FullName);
Console::WriteLine(S"{0};", Myfieldinfoa->GetValue(myfielda));
Console::WriteLine(S"IsFamilyOrAssembly = {0};", __box(Myfieldinfoa->IsFamilyOrAssembly));
if (Myfieldinfoa->IsFamilyOrAssembly )
Console::WriteLine(S"Field has limited accessibility");
// For the second field, get and display the name and field.
Console::WriteLine(S"\n{0} - ", MyTypeb->FullName);
Console::WriteLine(S"{0}", Myfieldinfob->GetValue(myfieldb));
return 0;
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
参照
FieldInfo クラス | FieldInfo メンバ | System.Reflection 名前空間 | FieldAttributes