FieldInfo.IsPublic Property


Gets a value indicating 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.

Public fields are accessible everywhere their corresponding classes are visible.

The IsPublic property is set when the FieldAttributes.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. If the field is other than public, it is protected and cannot be readily accessed. To access a nonpublic field, set the BindingFlags to NonPublic, specify either BindingFlags.Instance or BindingFlags.Static, and use this for the GetField method.

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

using System;
using System.Reflection;

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

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

    class Myfieldinfo
    public static int Main()
        Myfielda Myfielda = new Myfielda();
        Myfieldb Myfieldb = new Myfieldb();

        // Get the Type and FieldInfo.
        Type MyTypea = typeof(Myfielda);
        FieldInfo Myfieldinfoa = MyTypea.GetField("SomeField", 
        Type MyTypeb = typeof(Myfieldb);
        FieldInfo Myfieldinfob = MyTypeb.GetField("SomeField");

        // Get and display the IsPublic and IsPrivate property values.
        Console.Write("\n{0}.", MyTypea.FullName);
        Console.Write("{0} - ", Myfieldinfoa.Name);
        Console.Write("{0}", Myfielda.Field);
        Console.Write("\n   IsPublic = {0}", Myfieldinfoa.IsPublic);
        Console.Write("\n   IsPrivate = {0}", Myfieldinfoa.IsPrivate);

        Console.Write("\n{0}.", MyTypeb.FullName);
        Console.Write("{0} - ", Myfieldinfob.Name);
        Console.Write("{0};", Myfieldb.SomeField);
        Console.Write("\n   IsPublic = {0}", Myfieldinfob.IsPublic);
        Console.Write("\n   IsPrivate = {0}", Myfieldinfob.IsPrivate);

        return 0;

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft