FieldInfo.IsPrivate 属性

获取一个值,通过该值指示此字段是否为私有字段。

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

语法

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

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

属性值

如果此字段为私有字段,则为 true;否则为 false

备注

私有字段只能从成员函数访问。

设置 FieldAttributes.Private 属性 (Attribute) 时设置 IsPrivate 属性 (Property)。

若要获取 IsPrivate 属性 (Property),请先获取 Type 类。从 Type 获取 FieldInfo。从 FieldInfo 获取 IsPrivate 属性 (Property)。若要访问非公共字段,请在 GetField 方法中将 BindingFlags 设置为 NonPublic 以及 StaticInstance

示例

下面的示例返回一个值,该值指示该类的字段是否为私有字段。

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Class [MyClass]
    Private myField As String
    Public myArray() As String = {"New York", "New Jersey"}

    Sub New()
        myField = "Microsoft"
    End Sub 'New

    ReadOnly Property GetField() As String
        Get
            Return myField
        End Get
    End Property
End Class '[MyClass]


Class FieldInfo_IsPrivate

    Public Shared Sub Main()
        Try
            ' Gets the type of MyClass.
            Dim myType As Type = GetType([MyClass])

            ' Gets the field information of MyClass.
            Dim myFields As FieldInfo() = myType.GetFields((BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Instance))

            Console.WriteLine(ControlChars.Cr & "Displaying whether the fields of {0} are private or not:" & ControlChars.Cr, myType)
            Console.WriteLine() 
            Dim i As Integer
            For i = 0 To myFields.Length - 1
                ' Check whether the field is private or not. 
                If myFields(i).IsPrivate Then
                    Console.WriteLine("{0} is a private field.", myFields(i).Name)
                Else
                    Console.WriteLine("{0} is not a private field.", myFields(i).Name)
                End If
            Next i
        Catch e As Exception
            Console.WriteLine("Exception : {0} ", e.Message.ToString())
        End Try
    End Sub 'Main
End Class 'FieldInfo_IsPrivate
using System;
using System.Reflection;

class MyClass
{
    private string myField;
    public string[] myArray = new string[] {"New York", "New Jersey"};
    MyClass()
    {
        myField = "Microsoft";
    }
    string GetField
    {
        get
        {
            return myField;
        }
    }
}

class FieldInfo_IsPrivate
{
    public static void Main()
    {
        try
        {
            // Gets the type of MyClass.
            Type myType = typeof(MyClass);

            // Gets the field information of MyClass.
            FieldInfo[] myFields = myType.GetFields(BindingFlags.NonPublic
                |BindingFlags.Public
                |BindingFlags.Instance);
      
            Console.WriteLine("\nDisplaying whether the fields of {0} are private or not:\n", myType);
            for(int i = 0; i < myFields.Length; i++)
            {
                // Check whether the field is private or not. 
                if(myFields[i].IsPrivate)
                    Console.WriteLine("{0} is a private field.", myFields[i].Name);
                else
                    Console.WriteLine("{0} is not a private field.", myFields[i].Name);
            }
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception : {0} " , e.Message);
        }
    }
}
using namespace System;
using namespace System::Reflection;

ref class MyClass
{
private:
   String^ myField;

public:
   array<String^>^myArray;
   MyClass()
   {
      myField = "Microsoft";
      array<String^>^s = {"New York","New Jersey"};
      myArray = s;
   }

   property String^ GetField 
   {
      String^ get()
      {
         return myField;
      }
   }
};

int main()
{
   try
   {
      // Gets the type of MyClass.
      Type^ myType = MyClass::typeid;

      // Gets the field information of MyClass.
      array<FieldInfo^>^myFields = myType->GetFields( static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Public | BindingFlags::Instance) );
      Console::WriteLine( "\nDisplaying whether the fields of {0} are private or not:\n", myType );
      for ( int i = 0; i < myFields->Length; i++ )
      {
         // Check whether the field is private or not. 
         if ( myFields[ i ]->IsPrivate )
                  Console::WriteLine( " {0} is a private field.", myFields[ i ]->Name );
         else
                  Console::WriteLine( " {0} is not a private field.", myFields[ i ]->Name );
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception : {0} ", e->Message );
   }
}
import System.*;
import System.Reflection.*;

class MyClass
{
    private String myField;
    public String myArray[] = new String[] { "New York", "New Jersey" };

    MyClass()
    {
        myField = "Microsoft";
    } //MyClass

    /** @property 
     */
    String get_GetField()
    {
        return myField;
    } // get_GetField
} //MyClass

class FieldInfoIsPrivate
{
    public static void main(String[] args)
    {
        try {
            // Gets the type of MyClass.
            Type myType = MyClass.class.ToType();

            // Gets the field information of MyClass.
            FieldInfo myFields[] = myType.GetFields(BindingFlags.NonPublic 
                | BindingFlags.Public | BindingFlags.Instance);
            Console.WriteLine("\nDisplaying whether the fields of {0} are" 
                + " private or not:\n", myType);
            for (int i = 0; i < myFields.length; i++) {
                // Check whether the field is private or not. 
                if (myFields[i].get_IsPrivate()) {
                    Console.WriteLine("{0} is a private field.",
                        myFields[i].get_Name());
                }
                else {
                    Console.WriteLine("{0} is not a private field.", 
                        myFields[i].get_Name());
                }
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception : {0} ", e.get_Message());
        }
    } //main
} //FieldInfoIsPrivate

平台

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 命名空间
BindingFlags 枚举
Type