This documentation is archived and is not being maintained.
Type.GetField Method
.NET Framework 1.1
Gets a specific field of the current Type.
Overload List
Searches for the field with the specified name.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Function GetField(String) As FieldInfo
[C#] public FieldInfo GetField(string);
[C++] public: FieldInfo* GetField(String*);
[JScript] public function GetField(String) : FieldInfo;
Searches for the specified field, using the specified binding constraints.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public MustOverride Function GetField(String, BindingFlags) As FieldInfo Implements IReflect.GetField
[C#] public abstract FieldInfo GetField(string, BindingFlags);
[C++] public: virtual FieldInfo* GetField(String*, BindingFlags) = 0;
[JScript] public abstract function GetField(String, BindingFlags) : FieldInfo;
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, C#, C++] Note This example shows how to use one of the overloaded versions of GetField. For other examples that might be available, see the individual overload topics.
[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.
See Also
Show: