共用方式為


FieldInfo.IsPrivate Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a value that indicates whether the field is private.

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

Syntax

'Declaration
Public ReadOnly Property IsPrivate As Boolean
public bool IsPrivate { get; }

Property Value

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

Exceptions

Exception Condition
MethodAccessException

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

Remarks

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.

Examples

The following example returns a value that indicates whether the field of the class is private.


Imports System.Reflection

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 Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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))

         outputBlock.Text += String.Format(ControlChars.Cr & "Displaying whether the fields of {0} are private or not:" & ControlChars.Cr, myType) & vbCrLf
         outputBlock.Text &= vbCrLf
         Dim i As Integer
         For i = 0 To myFields.Length - 1
            ' Check whether the field is private or not. 
            If myFields(i).IsPrivate Then
               outputBlock.Text += String.Format("{0} is a private field.", myFields(i).Name) & vbCrLf
            Else
               outputBlock.Text += String.Format("{0} is not a private field.", myFields(i).Name) & vbCrLf
            End If
         Next i
      Catch e As Exception
         outputBlock.Text += String.Format("Exception : {0} ", e.Message.ToString()) & vbCrLf
      End Try
   End Sub 'Main
End Class 'FieldInfo_IsPrivate
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";
      }
   }
}

Version Information

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

Platforms

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