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

Type.GetField Method (String)

Searches for the public field with the specified name.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public FieldInfo GetField (
	string name
)
public final FieldInfo GetField (
	String name
)
public final 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 public field with the specified name, if found; otherwise, a null reference (Nothing in Visual Basic).
Exception typeCondition

ArgumentNullException

name is a null reference (Nothing in Visual Basic).

The search for name is case-sensitive. The search includes public static and public instance fields.

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).

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 System;
using System.Reflection;

public class MyFieldClassA
{
    public string Field = "A Field";
}

public class MyFieldClassB
{
    private string field = "B Field";
    public string Field 
    {
        get
        {
            return field;
        }
        set
        {
            if (field!=value)
            {
                field=value;
            }
        }
    }
}

public class MyFieldInfoClass
{
    public static void Main()
    {
        MyFieldClassB myFieldObjectB = new MyFieldClassB();
        MyFieldClassA myFieldObjectA = new MyFieldClassA();

        Type myTypeA = typeof(MyFieldClassA);
        FieldInfo myFieldInfo = myTypeA.GetField("Field");

        Type myTypeB = typeof(MyFieldClassB);
        FieldInfo myFieldInfo1 = myTypeB.GetField("field", 
            BindingFlags.NonPublic | BindingFlags.Instance);

        Console.WriteLine("The value of the public field is: '{0}'", 
            myFieldInfo.GetValue(myFieldObjectA));
        Console.WriteLine("The value of the private field is: '{0}'", 
            myFieldInfo1.GetValue(myFieldObjectB));
    }
}


import System.*;
import System.Reflection.*;
import System.Security.*;

public class MyFieldClassA
{
    public String field = "A Field";

    /** @property 
     */
    public String get_Field()
    {
        return field;
    } //get_Field

    /** @property 
     */
    public void set_Field(String value)
    {
        if (!(field.Equals(value))) {
            field = value;
        }
    } //set_Field
} //MyFieldClassA

public class MyFieldClassB
{
    public String field = "B Field";

    /** @property 
     */
    public String get_Field()
    {
        return field;
    } //get_Field

    /** @property 
     */
    public void set_Field(String value)
    {
        if (!(field.Equals(value))) {
            field = value;
        }
    } //set_Field
} //MyFieldClassB

public class MyFieldInfoClass
{
    public static void main(String[] args)
    {
        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.get_Message());
        }
        catch (ArgumentNullException e) {
            Console.WriteLine("Exception Raised!");
            Console.WriteLine("Message :" + e.get_Message());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception Raised!");
            Console.WriteLine("Message :" + e.get_Message());
        }
    } //main
} //MyFieldInfoClass

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.