Type.GetField Method (String, BindingFlags)
Searches for the specified field, using the specified binding constraints.
[Visual Basic] Overloads Public MustOverride Function GetField( _ ByVal name As String, _ ByVal bindingAttr As BindingFlags _ ) As FieldInfo Implements IReflect.GetField [C#] public abstract FieldInfo GetField( string name, BindingFlags bindingAttr ); [C++] public: virtual FieldInfo* GetField( String* name, BindingFlags bindingAttr ) = 0; [JScript] public abstract function GetField( name : String, bindingAttr : BindingFlags ) : FieldInfo;
Parameters
- name
- The String containing the name of the data field to get.
- bindingAttr
- A bitmask comprised of one or more BindingFlags that specify how the search is conducted.
-or-
Zero, to return a null reference (Nothing in Visual Basic).
Return Value
A FieldInfo object representing the field that matches the specified requirements, if found; otherwise, a null reference (Nothing in Visual Basic).
Implements
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentNullException | name is a null reference (Nothing in Visual Basic). |
Remarks
The following table shows what members of a base class are returned by the Get methods when reflecting on a type.
| Member Type | Static | Non-Static |
|---|---|---|
| Constructor | No | No |
| Field | No | Yes. A field is always hide-by-name-and-signature. |
| Event | Not applicable | The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below. |
| Method | No | Yes. A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature. |
| Nested Type | No | No |
| Property | Not applicable | The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below. |
- Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. This is a binary comparison.
- For reflection, properties and events are hide-by-name-and-signature. If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.
- Custom attributes are not part of the common type system.
The following BindingFlags filter flags can be used to define which fields to include in the search:
- You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.
- Specify BindingFlags.Public to include public fields in the search.
- Specify BindingFlags.NonPublic to include non-public fields (that is, private and protected fields) in the search.
- Specify BindingFlags.FlattenHierarchy to include static members up the hierarchy.
The following BindingFlags modifier flags can be used to change how the search works:
- BindingFlags.IgnoreCase to ignore the case of name.
- BindingFlags.DeclaredOnly to search only the fields declared on the Type, not fields that were simply inherited.
See System.Reflection.BindingFlags for more information.
If the requested type is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns a null reference (Nothing in Visual Basic).
Example
[Visual Basic, C#, C++] The following example gets the Type object for the specified class, obtains the FieldInfo object for the field that matches the specified binding flags, and displays the value of the field.
[Visual Basic] Imports System Imports System.Reflection Imports System.Security Imports Microsoft.VisualBasic Public Class MyFieldClassA Public myField As String = "A Field" Public Property Field() As String Get Return myField End Get Set(ByVal Value As String) If myField <> value Then myField = value End If End Set End Property End Class 'MyFieldClassA Public Class MyFieldClassB Public myField As String = "B Field" Public Property Field() As String Get Return myField End Get Set(ByVal Value As String) If myField <> value Then myField = value End If End Set End Property End Class 'MyFieldClassB Public Class MyFieldInfoClass Public Shared Sub Main() Try Dim myFieldObjectB As New MyFieldClassB() Dim myFieldObjectA As New MyFieldClassA() Dim myTypeA As Type = Type.GetType("MyFieldClassA") Dim myFieldInfo As FieldInfo = myTypeA.GetField("myField") Dim myTypeB As Type = Type.GetType("MyFieldClassB") Dim myFieldInfo1 As FieldInfo = myTypeB.GetField("myField", BindingFlags.Public Or 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 e As SecurityException Console.WriteLine("An exception has occurred: ") Console.WriteLine(("Message :" + e.Message)) Catch e As ArgumentNullException Console.WriteLine("An exception has occurred: ") Console.WriteLine(("Message :" + e.Message)) Catch e As Exception Console.WriteLine("An exception has occurred: ") Console.WriteLine(("Message :" + e.Message)) End Try End Sub 'Main End Class 'MyFieldInfoClass [C#] using System; using System.Reflection; using System.Security; public class MyFieldClassA { public string field = "A Field"; public string Field { get { return field; } set { if(field!=value) { field=value; } } } } public class MyFieldClassB { public string field = "B Field"; public string Field { get { return field; } set { if(field!=value) { field=value; } } } } public class MyFieldInfoClass { public static void Main() { try { MyFieldClassB myFieldObjectB = new MyFieldClassB(); MyFieldClassA myFieldObjectA = new MyFieldClassA(); Type myTypeA = Type.GetType("MyFieldClassA"); FieldInfo myFieldInfo = myTypeA.GetField("field"); Type myTypeB = Type.GetType("MyFieldClassB"); FieldInfo myFieldInfo1 = myTypeB.GetField("field", 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 :"+e.Message); } catch(ArgumentNullException e) { Console.WriteLine("Exception Raised!"); Console.WriteLine("Message :"+e.Message); } catch(Exception e) { Console.WriteLine("Exception Raised!"); Console.WriteLine("Message :"+e.Message); } } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::Reflection; using namespace System::Security; public __gc class MyFieldClassA { public: String* field; MyFieldClassA(){ field = S"A Field"; } __property String* get_Field() { return field; } __property void set_Field(String* value) { if (field!=value) { field=value; } } }; public __gc class MyFieldClassB { public: String* field; MyFieldClassB() { field = S"B Field"; } __property String* get_Field() { return field; } __property void set_Field(String* value) { if (field!=value) { field=value; } } }; int main() { try { MyFieldClassB* myFieldObjectB = new MyFieldClassB(); MyFieldClassA* myFieldObjectA = new MyFieldClassA(); Type* myTypeA = Type::GetType(S"MyFieldClassA"); FieldInfo* myFieldInfo = myTypeA->GetField(S"field"); Type* myTypeB = Type::GetType(S"MyFieldClassB"); FieldInfo* myFieldInfo1 = myTypeB->GetField(S"field", static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance)); Console::WriteLine(S"The value of the field is : {0} ", myFieldInfo->GetValue(myFieldObjectA)); Console::WriteLine(S"The value of the field is : {0} ", myFieldInfo1->GetValue(myFieldObjectB)); } catch (SecurityException* e) { Console::WriteLine(S"Exception Raised!"); Console::WriteLine(S"Message : {0}", e->Message); } catch (ArgumentNullException* e) { Console::WriteLine(S"Exception Raised!"); Console::WriteLine(S"Message : {0}", e->Message); } catch (Exception* e) { Console::WriteLine(S"Exception Raised!"); Console::WriteLine(S"Message : {0}", e->Message); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
.NET Framework Security:
- ReflectionPermission for reflecting non-public objects. Associated enumeration: ReflectionPermissionFlag.TypeInformation
See Also
Type Class | Type Members | System Namespace | Type.GetField Overload List | FieldInfo | String | BindingFlags | DefaultBinder | ReflectionPermission | GetFields