Delegate AccessTools.FieldRef<T, F>
A readable/assignable reference delegate to an instance field of a class or static field (NOT an instance field of a struct)
public delegate ref F FieldRef<in T, F>(T instance = null);
The runtime instance to access the field (ignored and can be omitted for static fields)
A readable/assignable reference to the field
An arbitrary type if the field is static; otherwise the class that defines the field, or a parent class (including System.Object), implemented interface, or derived class of this type
The type of the field; or if the field's type is a reference type (a class or interface, NOT a struct or other value type), a type that System.Type.IsAssignableFrom(System.Type) that type; or if the field's type is an enum type, either that type or the underlying integral type of that enum type
This delegate cannot be used for instance fields of structs, since a struct instance passed to the delegate would be passed by value and thus would be a copy that only exists within the delegate's invocation. This is fine for a readonly reference, but makes assignment futile. Use AccessTools.StructFieldRef<T, F> instead.
T is not required to be the field's declaring type. It can be a parent class (including System.Object),
implemented interface, or a derived class of the field's declaring type ("
instanceOfT is FieldDeclaringType" must be possible).
F must be System.Type.IsAssignableFrom(System.Type) OR to the field's declaring type.
Technically, this allows
Nullable is only relevant for structs, and since only static fields of structs
are allowed for this delegate, and the instance passed to such a delegate is ignored, this hardly matters.
F is not required to be the field's field type, unless that type is a non-enum value type.
It can be a parent class (including
object) or implemented interface of the field's field type. It cannot be a derived class.
This variance is not allowed for value types, since that would require boxing/unboxing, which is not allowed for ref values.
Special case for enum types:
F can also be the underlying integral type of the enum type.
Specifically, for reference types,
F must be System.Type.IsAssignableFrom(System.Type)
the field's field type; for non-enum value types,
F must be exactly the field's field type; for enum types,
F must be either the field's field type or the underyling integral type of that field type.
This delegate supports static fields, even those defined in structs, for legacy reasons.
For such static fields,
T is effectively ignored.
Consider using AccessTools.FieldRef<F> (and
StaticFieldRefAccess methods that return it) instead for static fields.