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)

'Declaration
Public ReadOnly Property IsPrivate As Boolean

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.



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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft