Field CLASS

A field member defined on a type.

A field can be either static or instance-scoped, with the latter representing a piece of data shared among the instances of that class. When accessing to a field through reflection, an instance field requires specifying the access target, namely the instance on which the field is to be accessed.

There are two operations that can be done on a field: getting the data from it and setting data to it.

  class Box {
    private int value = 5;
  }

  Field f = typeof(Box).getField("value");
  Box box = new Box();
  int val = f.get(box); // get 5
  f.set(box, 7); // set box.value to 7

Parent Class

Parent Interfaces

All Members


TypeNameSignature
methodgetpublic var get(var)
methodgetAttributespublic Attribute[] getAttributes()
methodgetKindpublic MemberKind getKind()
methodgetNamepublic String getName()
methodgetTypepublic Type getType()
methodisConstpublic bool isConst()
methodisStaticpublic bool isStatic()
methodsetpublic void set(var, var)
methodtoStringpublic String toString()

Methods


public var get(var instance)

Get the value held by this field on the given instance.

If this field is static, the argument is disregarded. If this field is instance-scoped, the argument must be of either the same type as the field's defining type, or a type that derives from the defining type. In the case of latter, it's also required that the field be visible to the class to which the object belongs. In all other cases ReflectedInvocationException will be thrown.

Parameters

  • instance The instance to retrieve the field from. Disregarded if it's a static field.

Returns

  • The value held by this field on the instance target.

Throws


public Attribute[] getAttributes()

(INHERITED DOC)

Get all the attributes annotated on the member definition.

The result array contains each instance of Attribute placed at the type definition.

Returns

  • The attributes annotated on this member. In case of members which do not have attributes, this returns a 0-sized Attribute array.

public MemberKind getKind()

A method's kind is FIELD.

Returns


public String getName()

Get the name of this field.

Returns

  • The name of this field.

public Type getType()

Get the type of this field.

Returns

  • The field's type.

public bool isConst()

Whether this field is constant. A constant field cannot be mutated after initialization.

Returns

  • True if the field is constant.

public bool isStatic()

Whether this field is static or instance-scoped.

Returns

  • True if the field is static; false instance-scoped.

public void set(var instance, var value)

Set the value to this field on the given instance.

If this field is static, the first argument is disregarded. If this field is instance-scoped, the argument must be of either the same type as the field's defining type, or a type that derives from the defining type. In the case of latter, it's also required that the field be visible to the class to which the object belongs. In all other cases ReflectedInvocationException will be thrown.

Parameters

  • instance The instance to retrieve the field from. Disregarded if it's a static field.
  • value The value to set to this field.

Throws

  • System.Reflection.ReflectedInvocationException Can be thrown when calling on an instance field with illegal instance target, such as null or having a type not compatible with the field's defining type. Can also be thrown due to an actual error raised from variable assignment, in which case this exception was created as a wrapper and the caller must call getCause() to get the original exception.

public String toString()

Get a string representation of this field member.

Returns

  • A string in form of [FIELD|name:type]. Example: [FIELD|value:int]