FieldInfo.IsPublic Property

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

Gets a value that indicates whether the field is public.

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

Syntax

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

Property Value

Type: System.Boolean
true if this field is public; otherwise, false.

Exceptions

Exception Condition
MethodAccessException

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

Remarks

Public fields are accessible everywhere their corresponding classes are visible.

The IsPublic property is set when the Public attribute is set.

To get the IsPublic property, first get the class Type. From the Type, get the FieldInfo. From the FieldInfo, get the IsPublic property.

Examples

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

Imports System.Reflection


' Make two fields.
Public Class Myfielda
   ' Make a private field.
   Private SomeField As String = "private field"

   Public ReadOnly Property Field() As String
      Get
         Return SomeField
      End Get
   End Property
End Class 'Myfielda

Public Class Myfieldb
   ' Make a public field.
   Public SomeField As String = "public field"
End Class 'Myfieldb

Public Class Example

   Public Shared Function Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) As Integer
      outputBlock.Text &= "Reflection.FieldInfo" & vbCrLf
      outputBlock.Text &= vbCrLf
      Dim Myfielda As New Myfielda()
      Dim Myfieldb As New Myfieldb()

      ' Get the Type and FieldInfo.
      Dim MyTypea As Type = GetType(Myfielda)
      Dim Examplea As FieldInfo = MyTypea.GetField("SomeField", _
          BindingFlags.NonPublic Or BindingFlags.Instance)
      Dim MyTypeb As Type = GetType(Myfieldb)
      Dim Exampleb As FieldInfo = MyTypeb.GetField("SomeField")

      ' Get and display the IsPublic and IsPrivate property values.
      outputBlock.Text &= String.Format("{0}.{1} - {2}", MyTypea.FullName, Examplea.Name, _
          Myfielda.Field) & vbCrLf
      outputBlock.Text &= String.Format("   IsPublic = {0}", Examplea.IsPublic) & vbCrLf
      outputBlock.Text &= String.Format("   IsPrivate = {0}", Examplea.IsPrivate) & vbCrLf
      outputBlock.Text &= vbCrLf
      outputBlock.Text &= String.Format("{0}.{1} - {2}", MyTypeb.FullName, Exampleb.Name, _
          Myfieldb.SomeField) & vbCrLf
      outputBlock.Text &= String.Format("   IsPublic = {0}", Exampleb.IsPublic) & vbCrLf
      outputBlock.Text &= String.Format("   IsPrivate = {0}", Exampleb.IsPrivate) & vbCrLf

      Return 0
   End Function 'Main
End Class 'Myfieldinfo
using System;
using System.Reflection;


// Make two fields.
public
    class Myfielda // private
{
   private string SomeField = "private field";
   public string Field
   {
      get { return SomeField; }
   }
}

public
    class Myfieldb // public
{
   public string SomeField = "public field";
}

public
    class Example
{
   public static int Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += "\nReflection.FieldInfo" + "\n";
      Myfielda Myfielda = new Myfielda();
      Myfieldb Myfieldb = new Myfieldb();

      // Get the Type and FieldInfo.
      Type MyTypea = typeof(Myfielda);
      FieldInfo Examplea = MyTypea.GetField("SomeField",
          BindingFlags.NonPublic | BindingFlags.Instance);
      Type MyTypeb = typeof(Myfieldb);
      FieldInfo Exampleb = MyTypeb.GetField("SomeField");

      // Get and display the IsPublic and IsPrivate property values.
      outputBlock.Text += String.Format("\n{0}.", MyTypea.FullName);
      outputBlock.Text += String.Format("{0} - ", Examplea.Name);
      outputBlock.Text += String.Format("{0}", Myfielda.Field);
      outputBlock.Text += String.Format("\n   IsPublic = {0}", Examplea.IsPublic);
      outputBlock.Text += String.Format("\n   IsPrivate = {0}", Examplea.IsPrivate);

      outputBlock.Text += String.Format("\n{0}.", MyTypeb.FullName);
      outputBlock.Text += String.Format("{0} - ", Exampleb.Name);
      outputBlock.Text += String.Format("{0};", Myfieldb.SomeField);
      outputBlock.Text += String.Format("\n   IsPublic = {0}", Exampleb.IsPublic);
      outputBlock.Text += String.Format("\n   IsPrivate = {0}", Exampleb.IsPrivate);

      return 0;
   }
}

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.