MSDN Library
EN
Il contenuto non è disponibile nella lingua di destinazione, ma solo in inglese.

FieldInfo.IsPrivate Property

Gets a value indicating whether the field is private.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)

public bool IsPrivate { get; }
/** @property */
public final boolean get_IsPrivate ()

public final function get IsPrivate () : boolean

Not applicable.

Property Value

true if the field is private; otherwise; false.

Private fields are accessible only from member functions.

The IsPrivate property is set when the FieldAttributes.Private attribute is set.

To get the IsPrivate property, first get the class Type. From the Type, get the FieldInfo. From the FieldInfo, get the IsPrivate property. To access a non-public field, set the BindingFlags to NonPublic, and either Static or Instance in the GetField method.

The following example returns a value indicating whether or not the field of the class is private.

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

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 Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Aggiunte alla community

AGGIUNGI
Mostra:
© 2016 Microsoft