FieldInfo::IsPublic Property
Gets a value indicating whether the field is public.
Assembly: mscorlib (in mscorlib.dll)
Implements
_FieldInfo::IsPublicPublic 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 namespace System; using namespace System::Reflection; // Make two fields. // private public ref class Myfielda { private: String^ SomeField; public: Myfielda() : SomeField( "private field" ) {} property String^ Field { String^ get() { return SomeField; } } }; // public public ref class Myfieldb { public: String^ SomeField; Myfieldb() : SomeField( "public field" ) {} property String^ Field { String^ get() { return SomeField; } } }; int main() { Console::WriteLine( "\nReflection.FieldInfo" ); Myfielda^ myfielda = gcnew Myfielda; Myfieldb^ myfieldb = gcnew Myfieldb; // Get the Type and FieldInfo. Type^ MyTypea = Type::GetType( "Myfielda" ); FieldInfo^ Myfieldinfoa = MyTypea->GetField( "SomeField", static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance) ); Type^ MyTypeb = Type::GetType( "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->Field ); Console::Write( "\n IsPublic = {0}", Myfieldinfob->IsPublic ); Console::Write( "\n IsPrivate = {0}", Myfieldinfob->IsPrivate ); return 0; }
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1