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.
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| 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.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |
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; } }
Note: