IsPublic Property

FieldInfo.IsPublic 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 public.

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

public bool IsPublic { get; }

Property Value

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

ExceptionCondition
MethodAccessException

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

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.

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


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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft