FieldInfo.IsPrivate Właściwość

Definicja

Pobiera wartość wskazującą, czy pole jest prywatne.

C#
public bool IsPrivate { get; }

Wartość właściwości

true jeśli pole jest prywatne; Inaczej; false.

Implementuje

Przykłady

Poniższy przykład zwraca wartość wskazującą, czy pole klasy jest prywatne.

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

Uwagi

Pola prywatne są dostępne tylko z funkcji członkowskich.

Właściwość IsPrivate jest ustawiana po ustawieniu atrybutu FieldAttributes.Private .

Aby uzyskać IsPrivate właściwość , najpierw pobierz klasę Type. W pliku Typepobierz element FieldInfo. W pliku FieldInfopobierz IsPrivate właściwość . Aby uzyskać dostęp do pola niepublikowego, ustaw parametr BindingFlags na , lub StaticInstance w metodzie GetFieldNonPublic.

Dotyczy

Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Zobacz też