Type::GetField Method (String)
Updated: September 2009
Searches for the public field with the specified name.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- name
- Type: System::String
The String containing the name of the data field to get.
Return Value
Type: System.Reflection::FieldInfoA FieldInfo object representing the public field with the specified name, if found; otherwise, nullptr.
Implements
_Type::GetField(String)| Exception | Condition |
|---|---|
| ArgumentNullException | name is nullptr. |
| NotSupportedException | This Type object is a TypeBuilder whose CreateType method has not yet been called. |
The search for name is case-sensitive. The search includes public static and public instance fields.
If the current Type represents a constructed generic type, this method returns the FieldInfo with the type parameters replaced by the appropriate type arguments.
If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the fields of the class constraint.
The following example gets the Type object for the specified class, obtains the FieldInfo object for the field, and displays the value of the field.
using namespace System; using namespace System::Reflection; using namespace System::Security; public ref class MyFieldClassA { public: String^ field; MyFieldClassA() { field = "A Field"; } property String^ Field { String^ get() { return field; } void set( String^ value ) { if ( field != value ) { field = value; } } } }; public ref class MyFieldClassB { public: String^ field; MyFieldClassB() { field = "B Field"; } property String^ Field { String^ get() { return field; } void set( String^ value ) { if ( field != value ) { field = value; } } } }; int main() { try { MyFieldClassB^ myFieldObjectB = gcnew MyFieldClassB; MyFieldClassA^ myFieldObjectA = gcnew MyFieldClassA; Type^ myTypeA = Type::GetType( "MyFieldClassA" ); FieldInfo^ myFieldInfo = myTypeA->GetField( "field" ); Type^ myTypeB = Type::GetType( "MyFieldClassB" ); FieldInfo^ myFieldInfo1 = myTypeB->GetField( "field", static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) ); Console::WriteLine( "The value of the field is : {0} ", myFieldInfo->GetValue( myFieldObjectA ) ); Console::WriteLine( "The value of the field is : {0} ", myFieldInfo1->GetValue( myFieldObjectB ) ); } catch ( SecurityException^ e ) { Console::WriteLine( "Exception Raised!" ); Console::WriteLine( "Message : {0}", e->Message ); } catch ( ArgumentNullException^ e ) { Console::WriteLine( "Exception Raised!" ); Console::WriteLine( "Message : {0}", e->Message ); } catch ( Exception^ e ) { Console::WriteLine( "Exception Raised!" ); Console::WriteLine( "Message : {0}", e->Message ); } }
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Date | History | Reason |
|---|---|---|
September 2009 | Removed an erroneous statement that nullptr is returned for non-public members outside the assembly, if caller lacks ReflectionPermission. | Content bug fix. |