FieldInfo.IsFamilyOrAssembly プロパティ
アセンブリ: mscorlib (mscorlib.dll 内)
構文
フィールドに FamORAssem 属性が設定されている場合は true。それ以外の場合は false。
フィールドに FamilyOrAssembly レベルの参照可能範囲が設定されている場合、そのフィールドは派生クラスのすべてのメンバ、または同じアセンブリ内の任意のメンバから呼び出せますが、他の型からは呼び出せません。
IsFamilyOrAssembly プロパティは、FieldAttributes.FamORAssem 属性が設定されたときに設定されます。
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 = GetType(Myfielda) Dim Myfieldinfoa As FieldInfo = MyTypea.GetField("field", BindingFlags.NonPublic Or BindingFlags.Instance) Dim MyTypeb As 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
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 = typeof(Myfielda); FieldInfo Myfieldinfoa = MyTypea.GetField("field", BindingFlags.NonPublic|BindingFlags.Instance); Type MyTypeb = typeof(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; } }
using namespace System; using namespace System::Reflection; //Make two fields. // 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. public ref class Myfielda { public protected: // private: String^ field; public: Myfielda() : field( "A private field" ) {} property String^ Field { String^ get() { return field; } void set( 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 ref class Myfieldb: public Myfielda { public: property String^ Field { String^ get() { return field; } void set( String^ value ) { if ( field != value ) { field = value; } } } }; int main() { Console::WriteLine( "\nReflection.FieldInfo" ); Myfielda^ myfielda = gcnew Myfielda; Myfieldb^ myfieldb = gcnew Myfieldb; // Get the Type and FieldInfo. Type^ MyTypea = Type::GetType( "Myfielda" ); FieldInfo^ Myfieldinfoa = MyTypea->GetField( "field", static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance) ); Type^ MyTypeb = Type::GetType( "Myfieldb" ); FieldInfo^ Myfieldinfob = MyTypeb->GetField( "field", static_cast<BindingFlags>(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; }
import System.*; import 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 String field = "A private field"; /** @property */ public String get_Field() { return field; } //get_Field /** @property */ public void set_Field(String value) { if (field != value) { field = value; } } //set_Field } //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 extends MyFieldA { /** @property */ public String get_Field() { return field; } //get_Field /** @property */ public void set_Field(String value) { if (field != value) { field = value; } } //set_Field } //MyFieldB public class MyFieldInfo { public static void main(String[] args) { 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.get_FullName()); Console.WriteLine("{0};", myFieldInfoA.GetValue(myFieldA)); Console.WriteLine("IsFamilyOrAssembly = {0};", (System.Boolean)myFieldInfoA.get_IsFamilyOrAssembly()); if (myFieldInfoA.get_IsFamilyOrAssembly()) { Console.WriteLine("Field has limited accessibility"); } // For the second field, get and display the name and field. Console.WriteLine("\n{0} - ", myTypeB.get_FullName()); Console.WriteLine("{0}", myFieldInfoB.GetValue(myFieldB)); return; } //main } //MyFieldInfo
//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 as Myfielda.field is inaccessible. { //private string field = "A private field"; protected internal var field : String = "A private field"; } //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 extends Myfielda { public function get Field() : String { return field; } public function set Field(value : String) { if(field!=value) field=value; } } public class Myfieldinfo { public static function Main() : void { Console.WriteLine("\nReflection.FieldInfo"); var myfielda : Myfielda = new Myfielda(); var myfieldb : Myfieldb = new Myfieldb(); //Get the Type and FieldInfo. var MyTypea : Type = Type.GetType("Myfielda"); var Myfieldinfoa : FieldInfo = MyTypea.GetField("field" , BindingFlags.NonPublic|BindingFlags.Instance); var MyTypeb : Type = Type.GetType("Myfieldb"); var Myfieldinfob : FieldInfo = MyTypeb.GetField("field" , BindingFlags.NonPublic|BindingFlags.Instance); //For the first field, get and display the Name, field, and //IsFamilyOrAssembly. Console.Write("\n{0} - ", MyTypea.FullName); Console.Write("{0};", Myfieldinfoa.GetValue(myfielda)); Console.Write("\n IsFamilyOrAssembly = {0};", Myfieldinfoa.IsFamilyOrAssembly); if (Myfieldinfoa.IsFamilyOrAssembly ) Console.Write("Field has limited accessibility"); //For the second field, get and display the name and field. Console.Write("\n{0} - ", MyTypeb.FullName); Console.Write("{0}; ", Myfieldinfob.GetValue(myfieldb)); } } Myfieldinfo.Main();
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
_FieldInfo.IsFamilyOrAssembly プロパティ
アセンブリ: mscorlib (mscorlib.dll 内)
構文
フィールドに FamORAssem 属性が設定されている場合は true。それ以外の場合は false。
このプロパティは、アンマネージ コードからマネージ クラスにアクセスするためのプロパティであるため、マネージ コードからは呼び出さないでください。
IsFamilyOrAssembly プロパティは、このフィールドに FamilyOrAssembly レベルの参照範囲が設定されているかどうかを示す値を取得します。
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- _FieldInfo.IsFamilyOrAssemblyのページへのリンク