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
Type | Name | Signature |
---|---|---|
method | get | public var get(var) |
method | getAttributes | public Attribute[] getAttributes() |
method | getKind | public MemberKind getKind() |
method | getName | public String getName() |
method | getType | public Type getType() |
method | isConst | public bool isConst() |
method | isStatic | public bool isStatic() |
method | set | public void set(var, var) |
method | toString | public String toString() |
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
Returns
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
public MemberKind getKind()
A method's kind is FIELD.
Returns
public String getName()
Get the name of this field.
Returns
public Type getType()
Get the type of this field.
Returns
public bool isConst()
Whether this field is constant. A constant field cannot be mutated after initialization.
Returns
public bool isStatic()
Whether this field is static or instance-scoped.
Returns
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
Throws
public String toString()
Get a string representation of this field member.
Returns
[FIELD|name:type]
. Example: [FIELD|value:int]