Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Type.GetField Method (String)

.NET Framework 1.1

Searches for the field with the specified name.

[Visual Basic]
Overloads Public Function GetField( _
   ByVal name As String _
) As FieldInfo
[C#]
public FieldInfo GetField(
 string name
);
[C++]
public: FieldInfo* GetField(
 String* name
);
[JScript]
public function GetField(
   name : String
) : FieldInfo;

Parameters

name
The String containing the name of the data field to get.

Return Value

A FieldInfo object representing the field with the specified name, if found; otherwise, a null reference (Nothing in Visual Basic).

Exceptions

Exception Type Condition
ArgumentNullException name is a null reference (Nothing in Visual Basic).

Remarks

The search for name is case-sensitive.

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, 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 Language Filter 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: 

See Also

Type Class | Type Members | System Namespace | Type.GetField Overload List | FieldInfo | String | DefaultBinder | ReflectionPermission | GetFields

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.