Browse documentation

Delegate AccessTools.FieldRef<T, F>

Namespace
HarmonyLib
Assembly
0Harmony.dll

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 AccessTools.FieldRef<in T, F>(T instance = default)

Parameters

instance T

The runtime instance to access the field (ignored and can be omitted for static fields)

Returns

F

A readable/assignable reference to the field

Type Parameters

T

An arbitrary type if the field is static; otherwise the class that defines the field, or a parent class (including 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 is assignable from that type; or if the field's type is an enum type, either that type or the underlying integral type of that enum 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 object), implemented interface, or a derived class of the field's declaring type ("instanceOfT is FieldDeclaringType" must be possible). Specifically, F must be assignable from 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 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 assignable from 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.

Constructors

FieldRef(object, IntPtr)

public FieldRef(object @object, IntPtr method)

Parameters

object object
method IntPtr

Exceptions

NullReferenceException

Null instance passed to a non-static field ref delegate

InvalidCastException

Instance of invalid type passed to a non-static field ref delegate (this can happen if T is a parent class or interface of the field's declaring type)

Methods

BeginInvoke(T, AsyncCallback, object)

public virtual IAsyncResult BeginInvoke(T instance, AsyncCallback callback, object @object)

Parameters

instance T
callback AsyncCallback
object object

Returns

IAsyncResult

Exceptions

NullReferenceException

Null instance passed to a non-static field ref delegate

InvalidCastException

Instance of invalid type passed to a non-static field ref delegate (this can happen if T is a parent class or interface of the field's declaring type)

EndInvoke(IAsyncResult)

public virtual ref F EndInvoke(IAsyncResult result)

Parameters

result IAsyncResult

Returns

F

Exceptions

NullReferenceException

Null instance passed to a non-static field ref delegate

InvalidCastException

Instance of invalid type passed to a non-static field ref delegate (this can happen if T is a parent class or interface of the field's declaring type)

Invoke(T)

public virtual ref F Invoke(T instance = default)

Parameters

instance T

Returns

F

Exceptions

NullReferenceException

Null instance passed to a non-static field ref delegate

InvalidCastException

Instance of invalid type passed to a non-static field ref delegate (this can happen if T is a parent class or interface of the field's declaring type)

Harmony 3 preview

For the stable release, read the 2.x documentation.