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)
Namespace: HarmonyLib
Assembly: 0Harmony.dll
Syntax
public delegate F FieldRef<T, F>(T instance = null);
Parameters
| Type | Name | Description |
|---|---|---|
| T | instance | The runtime instance to access the field (ignored and can be omitted for static fields) |
Returns
| Type | Description |
|---|---|
| F | A readable/assignable reference to the field |
Type Parameters
| Name | Description |
|---|---|
| T | 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 |
| F | 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) the field's type |
Remarks
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.
Note that 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).
Specifically, F must be System.Type.IsAssignableFrom(System.Type) OR to the field's declaring type.
Technically, this allows Nullable, although 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.
Similarly, F is not required to be the field's field type, unless that type is a 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.
Specifically, for reference types, F must be System.Type.IsAssignableFrom(System.Type)
the field's field type; and for value types, F must be exactly the field's field type.
This delegate supports static fields, even those defined in structs, for legacy reasons.
For such static fields, T is effectively ignored.
This is also the reason that this delegate lacks a generic class constraint (it was added to certain FieldRefAccess methods,
but such a constraint cannot be added to this delegate without breaking binary compatibility).
Consider using AccessTools.FieldRef<F> (and StaticFieldRefAccess methods that return it) instead for static fields.