Export (0) Print
Expand All

FieldInfo.IsPrivate Property

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


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft