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.
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| 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.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |
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
Note: