FieldInfo.IsPublic Property

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


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

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

Community Additions

ADD
Show: