FieldInfo.IsPrivate FieldInfo.IsPrivate FieldInfo.IsPrivate FieldInfo.IsPrivate Property

Définition

Obtient une valeur indiquant si le champ est privé.Gets a value indicating whether the field is private.

public:
 property bool IsPrivate { bool get(); };
public bool IsPrivate { get; }
member this.IsPrivate : bool
Public ReadOnly Property IsPrivate As Boolean

Valeur de propriété

true si le champ est privé ; sinon, false.true if the field is private; otherwise; false.

Implémente

Exemples

L’exemple suivant retourne une valeur indiquant si le champ de la classe est privé ou non.The following example returns a value indicating whether or not the field of the class is private.

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 );
   }
}
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);
        }
    }
}

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

Remarques

Les champs privés sont accessibles uniquement à partir de fonctions membres.Private fields are accessible only from member functions.

Le IsPrivate propriété est définie lorsque le FieldAttributes.Private attribut est défini.The IsPrivate property is set when the FieldAttributes.Private attribute is set.

Pour obtenir le IsPrivate propriété, commencez par obtenir la classe Type.To get the IsPrivate property, first get the class Type. À partir de la Type, obtenir le FieldInfo.From the Type, get the FieldInfo. À partir de la FieldInfo, obtenir le IsPrivate propriété.From the FieldInfo, get the IsPrivate property. Pour accéder à un champ non public, définissez le BindingFlags à NonPublicet la valeur Static ou Instance dans le GetField (méthode).To access a non-public field, set the BindingFlags to NonPublic, and either Static or Instance in the GetField method.

S’applique à

Voir aussi