IsPrivate Property

FieldInfo.IsPrivate Property

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Gets a value that indicates whether the field is private.

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

public bool IsPrivate { get; }

Property Value

Type: System.Boolean
true if the field is private; otherwise; false.

ExceptionCondition
MethodAccessException

This member is invoked late-bound through mechanisms such as Type.InvokeMember.

Private fields are accessible only from member functions.

The IsPrivate property is set when the 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.

The following example returns a value that indicates whether 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 Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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);

         outputBlock.Text += String.Format("\nDisplaying whether the fields of {0} are private or not:\n", myType) + "\n";
         for (int i = 0; i < myFields.Length; i++)
         {
            // Check whether the field is private or not. 
            if (myFields[i].IsPrivate)
               outputBlock.Text += String.Format("{0} is a private field.", myFields[i].Name) + "\n";
            else
               outputBlock.Text += String.Format("{0} is not a private field.", myFields[i].Name) + "\n";
         }
      }
      catch (Exception e)
      {
         outputBlock.Text += String.Format("Exception : {0} ", e.Message) + "\n";
      }
   }
}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft