FieldInfo.IsFamilyOrAssembly 属性

获取一个值,通过该值指示此字段是否有 FamilyOrAssembly 级可见性。

**命名空间:**System.Reflection
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public ReadOnly Property IsFamilyOrAssembly As Boolean
用法
Dim instance As FieldInfo
Dim value As Boolean

value = instance.IsFamilyOrAssembly
public bool IsFamilyOrAssembly { get; }
public:
virtual property bool IsFamilyOrAssembly {
    bool get () sealed;
}
/** @property */
public final boolean get_IsFamilyOrAssembly ()
public final function get IsFamilyOrAssembly () : boolean

属性值

如果字段设置了 FamORAssem 属性 (Attribute),则为 true;否则为 false

备注

如果字段具有 FamilyOrAssembly 级可见性,则可以从派生类中的任何成员或同一程序集中的任何成员调用此字段,但不能从其他任何类型调用此字段。

设置 FieldAttributes.FamORAssem 属性 (Attribute) 时设置 IsFamilyOrAssembly 属性 (Property)。

示例

在下面的示例中将创建两个字段。第二个字段 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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

FieldInfo 类
FieldInfo 成员
System.Reflection 命名空间
FieldAttributes 枚举