FieldInfo.IsPublic Property
Gets a value indicating whether the field is public.
Assembly: mscorlib (in mscorlib.dll)
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. 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 Myfieldinfo { public static int Main() { Console.WriteLine("\nReflection.FieldInfo"); Myfielda Myfielda = new Myfielda(); Myfieldb Myfieldb = new Myfieldb(); // Get the Type and FieldInfo. Type MyTypea = typeof(Myfielda); FieldInfo Myfieldinfoa = MyTypea.GetField("SomeField", BindingFlags.NonPublic|BindingFlags.Instance); 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; } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.