System.Type.GetField Method

Searches for the specified field, using the specified binding constraints.

Syntax

public abstract System.Reflection.FieldInfo GetField (string name, System.Reflection.BindingFlags bindingAttr)

Parameters

name
The string containing the name of the data field to get.
bindingAttr
A bitmask comprised of one or more System.Reflection.BindingFlags that specify how the search is conducted.

Returns

An object representing the field that matches the specified requirements, if found; otherwise, null.

Exceptions

TypeReason
ArgumentNullException name is null.

Remarks

The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

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.

[The 'ordered' type of list has not been implemented in the ECMA stylesheet.]

The following System.Reflection.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, internal, and protected fields) in the search.

  • Specify BindingFlags.FlattenHierarchy to include public and protected static members up the hierarchy; private static members in inherited classes are not included.

The following System.Reflection.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 current Type represents a constructed generic type, this method returns the System.Reflection.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.

Permissions

TypeReason
System.Security.Permissions.ReflectionPermissionRequires permission to retrieve information on non-public members of types in loaded assemblies. See System.Security.Permissions.ReflectionPermissionFlag.TypeInformation.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0