FieldInfo.IsFamily 属性

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

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

语法

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

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

属性值

如果此字段具有 Family 属性 (Attribute) 设置,则为 true;否则为 false

备注

可以从派生类中的任何成员调用此属性 (Property),但不能从任何其他类型调用此属性 (Property)。

若要获取 IsFamily 属性 (Property),请先获取 Type 类。从 Type 获取 FieldInfo。从 FieldInfo 获取 IsFamily 值。

设置 FieldAttributes.Family 属性 (Attribute) 时设置 IsFamily 属性 (Property)。

示例

在下面的示例中,创建了两个属性 (Property)。第一个属性 (Property) 有一个私有字符串字段,第二个属性 (Property) 有一个受保护的字符串字段,从而将 IsFamily 属性设置为 true

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic


'Make two fields.

Public Class Myfielda
    Private 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

Public Class Myfieldb
    Protected field As String = "B protected 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 'Myfieldb

Public Class Myfieldinfo

    Public Shared Function Main() As Integer
        Console.WriteLine(ControlChars.Lf & "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 IsFamily.
        Console.Write(ControlChars.Lf & "{0} - ", MyTypea.FullName)
        Console.Write("{0} - ", Myfieldinfoa.GetValue(Myfielda))
        Console.Write(ControlChars.Lf & "  IsFamily - {0}", Myfieldinfoa.IsFamily)
        Console.Write(ControlChars.Lf & "  FieldAttributes - {0}", _
            Myfieldinfoa.Attributes.ToString())

        ' For the second field, get and Display the Name, field, and IsFamily.
        Console.WriteLine()
        Console.Write(ControlChars.Lf & "{0} - ", MyTypeb.FullName)
        Console.Write("{0} - ", Myfieldinfob.GetValue(Myfieldb))
        Console.Write(ControlChars.Lf & "  IsFamily - {0}", Myfieldinfob.IsFamily)
        Dim Myfieldattributesb As FieldAttributes = Myfieldinfob.Attributes
        Console.Write(ControlChars.Lf & "  FieldAttributes - {0}", _
            Myfieldinfob.Attributes.ToString())

        Return 0
    End Function 'Main
End Class 'Myfieldinfo
using System;
using System.Reflection;

//Make two fields.
public class Myfielda
{
    private string field = "A private field";
    public string Field
    {
        get{return field;}
        set{if(field!=value){field=value;}}
    }
}
 
public class Myfieldb
{
    protected string field = "B protected field";
    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 IsFamily.
        Console.Write("\n{0} - ", MyTypea.FullName);
        Console.Write("{0} - ", Myfieldinfoa.GetValue(Myfielda));
        Console.Write("\n  IsFamily - {0}", Myfieldinfoa.IsFamily);
        Console.Write("\n  FieldAttributes - {0}",
            Myfieldinfoa.Attributes.ToString());
  
        // For the second field, get and Display the Name, field, and IsFamily.
        Console.WriteLine();
        Console.Write("\n{0} - ", MyTypeb.FullName);
        Console.Write("{0} - ", Myfieldinfob.GetValue(Myfieldb));
        Console.Write("\n  IsFamily - {0}", Myfieldinfob.IsFamily);
        FieldAttributes Myfieldattributesb = Myfieldinfob.Attributes;
        Console.Write("\n  FieldAttributes - {0}",
            Myfieldinfob.Attributes.ToString());
  
        return 0;
    }
}
using namespace System;
using namespace System::Reflection;

//Make two fields.
public ref class Myfielda
{
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;
         }
      }

   }

};

public ref class Myfieldb
{
protected:
   String^ field;

public:
   Myfieldb()
      : field( "B public field" )
   {}


   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 IsFamily.
   Console::Write( "\n{0} - ", MyTypea->FullName );
   Console::Write( "{0} - ", Myfieldinfoa->GetValue( myfielda ) );
   Console::Write( "\n  IsFamily - {0}", Myfieldinfoa->IsFamily );
   Console::Write( "\n  FieldAttributes - {0}", Myfieldinfoa->Attributes );
   
   // For the second field, get and Display the Name, field, and IsFamily.
   Console::WriteLine();
   Console::Write( "\n{0} - ", MyTypeb->FullName );
   Console::Write( "{0} - ", Myfieldinfob->GetValue( myfieldb ) );
   Console::Write( "\n  IsFamily - {0}", Myfieldinfob->IsFamily );
   Console::Write( "\n  FieldAttributes - {0}", Myfieldinfob->Attributes );
   return 0;
}
import System.*;
import System.Reflection.*;

//Make two fields.
public class MyFieldA
{
    private 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

public class MyFieldB
{
    protected String field = "B public field";

    /** @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 IsFamily.
        Console.Write("\n{0} - ", myTypeA.get_FullName());
        Console.Write("{0} - ", myFieldInfoA.GetValue(myFieldA));
        Console.Write("\n  IsFamily - {0}", 
            (System.Boolean)myFieldInfoA.get_IsFamily());
        Console.Write("\n  FieldAttributes - {0}", 
            myFieldInfoA.get_Attributes().ToString());

        // For the second field, get and Display the Name, field, and IsFamily.
        Console.WriteLine();
        Console.Write("\n{0} - ", myTypeB.get_FullName());
        Console.Write("{0} - ", myFieldInfoB.GetValue(myFieldB));
        Console.Write("\n  IsFamily - {0}",
            (System.Boolean)myFieldInfoB.get_IsFamily());
        FieldAttributes Myfieldattributesb = myFieldInfoB.get_Attributes();
        Console.Write("\n  FieldAttributes - {0}",
            myFieldInfoB.get_Attributes().ToString());

        return;
    } //main
} //MyFieldInfo
//Make two fields.
import System;
import System.Reflection;

public class Myfielda
{
   private var field : String = "A private field";
   public function get Field() : String {
       return field;
   }
   public function set Field(value : String) {    
       if (field!=value) field=value;
   }
}

public class Myfieldb
{
   protected var field : String = "B public field";
   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.Instance|BindingFlags.NonPublic);
      var MyTypeb : Type = Type.GetType("Myfieldb");
      var Myfieldinfob : FieldInfo = MyTypeb.GetField("field", BindingFlags.Instance|BindingFlags.NonPublic);
 
      //For the first field, get and display the Name, field, and IsFamily.
      Console.Write("\n{0} - ", MyTypea.FullName);
      Console.Write("{0} - ", Myfieldinfoa.GetValue(myfielda));
      Console.Write("\n  IsFamily - {0}", Myfieldinfoa.IsFamily);
      Console.Write("\n  FieldAttributes - {0}",
         Myfieldinfoa.Attributes.ToString());
 
      //For the second field, get and Display the Name, field, and IsFamily.
      Console.Write("\n{0} - ", MyTypeb.FullName);
      Console.Write("{0} - ", Myfieldinfob.GetValue(myfieldb));
      Console.Write("\n  IsFamily - {0}", Myfieldinfob.IsFamily);
      var Myfieldattributesb : FieldAttributes = Myfieldinfob.Attributes;
      Console.Write("\n  FieldAttributes - {0}",
         Myfieldinfob.Attributes.ToString());
   }
}
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 枚举
Type